Mockito错误“Only void methods can doNothing()”当尝试模拟一个void方法时

z31licg0  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(186)

我正在试验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>

相关问题