我使用下面的测试来测试我的utiliesclass,我使用mockito来进行sql连接。
@Mock
public Connection connectionMock;
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
}
@Test
public void testResource(){
String sql = Utilities.resourceToString("testSql.sql");
try {
Mockito.when(connectionMock.createStatement().executeQuery(sql)).thenAnswer(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocationOnMock) throws Throwable {
return "X";
}
});
我在Mockito行上得到了一个空指针。什么时候,什么地方出了问题?
3条答案
按热度按时间mrwjdhj31#
你还需要一次嘲弄...
...将返回null,除非您为其设置了预期值。
例如,添加...
更新
若要回答下面的注解,应返回结果集,而不是字符串。例如...
更新#2
删除错误的内容
8yoxcaq72#
你可以试着用
RETURNS_DEEP_STUBS
;在您的程式码中,它会看起来像这样:
d6kp6zgx3#
实际上,你可以有一个更好的用法关于Mockito: