随机Mockito Stubbing异常

时间:2016-06-23 14:27:42

标签: java unit-testing mocking mockito

我使用Mockito进行单元测试时得到以下代码片段,该片段已经过了好几个月/几年。

    @Test
    public void testAddRemoveTarFile() throws IOException, GeneralSecurityException {
    //add a TAR file
    TableRowCount downloadRowCount = new TableRowCount(tableDownloads);

    //create the item that will appear in the table row 
    MyHSAItem item = createMyHSAItem();
    Mockito.when(model.getConfigurations(Type.TAR)).thenReturn(Arrays.asList(item));

    //get the table model
    JTable downloadsTable = (JTable)UI.findComponent(getPanel(), "download");
    final MyHSATableModel tableModel = (MyHSATableModel ) downloadsTable.getModel();

    final MyHSAEvent event = Mockito.mock(MyHSAEvent.class);
    Mockito.when(event.getType()).thenReturn(MyHSAEvent.Type.MODEL);

    //Fire table event when adding observation
    final File xmlFile = Mockito.mock(File.class);
    Mockito.doAnswer(new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            tableModel.modelChanged(event);
            return null;
        }
    }).when(model).addObservation(xmlFile);

    //Fire table event when deleting observation
    Mockito.doAnswer(new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            tableModel.modelChanged(event);
            return null;
        }
    }).when(model).delete(item, true);

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            UI.findButtonWithText(getPanel(), "Add ...").doClick();
        }
    });

    //select a file, table row should update
    chooseFile(xmlFile);
    ensureEquals(1, downloadRowCount, TIMEOUT);

    // Remove download + cancel
    UI.leftClick(tableDownloads);
    clickRemove("Cancel");
    ensureEquals(1, downloadRowCount, TIMEOUT);

    // Remove download + OK
    UI.leftClick(tableDownloads);
    Mockito.when(model.getConfigurations(Type.TAR)).thenReturn(new ArrayList<MyHSAItem>());
    clickRemove("OK");
    ensureEquals(0, downloadRowCount, TIMEOUT);
}

突然间它只失败了一次:

org.mockito.exceptions.misusing.UnfinishedStubbingException:
Unfinished stubbing detected here:
-> at herschel.ia.pal.pool.hsa.gui.MyHsaPreferencePanelTest.testAddRemoveTarFile(MyHsaPreferencePanelTest.java:257)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. although stubbed methods may return mocks, you cannot inline mock creation (mock()) call inside a thenReturn method (see issue 53)

我理解这个错误,但不知道它是如何随机发生的。 Mockito.doAnswer似乎是个问题。我没有内嵌嘲讽,它似乎没问题,而且一直都有效。它能是什么?

第257行,

Mockito.doAnswer(new Answer<Void>() {

模型确实是一个初始化的字段:

@Mock
private MyHSANotifiableModelImpl model;

public void setUpPanel() {
    MockitoAnnotations.initMocks(this);

两个答案都返回null并且签名为Void,所以不确定你的意思。

感谢您的帮助

0 个答案:

没有答案