我正在试验Mockito,在尝试模拟一个void方法时遇到了一个错误:“只有void方法才能doNothing()!“。然而我试图嘲笑的方法是一个void方法:第一个月
这就是我正在做的
public class MockitoTest {
@Mock private Connection mockConnection;
@Mock private SQLServerPreparedStatement mockStatement;
@Test
public void mockitoTest() throws SQLException
{
MockitoAnnotations.openMocks(this);
Mockito.when(mockConnection.prepareStatement(Mockito.any())).thenReturn(mockStatement);
// Can mock this and it doesnt give me any errors
Mockito.doNothing().when(mockStatement).addBatch(Mockito.isA(String.class));
// This throws me the mentioned MockitoException, even through setStructured is void
Mockito.doNothing().when(mockStatement).setStructured(1, Mockito.isA(String.class), Mockito.isA(SQLServerDataTable.class));
// ...
}
字符串
根据我的评论,我可以模拟SQLServerPreparedStatement的addBatch方法,但不能模拟setStructured方法?两者都是void方法。为什么Mockito认为setStructured不是一个void方法?
我正在使用这个版本:
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
型
1条答案
按热度按时间zfycwa2u1#
谢谢@Gimby指出这个问题。这实际上与最终的方法有关。
一些参考文献帮助了我: