单元测试 - mockito测试以检查是否调用了另一个方法

时间:2014-04-17 23:36:01

标签: unit-testing junit mockito

总结:我试图测试一个方法在调用一个方法后是否被调用。

本课程的作用是显示错误拼写单词的信息,并为您提供“忽略”或“忽略所有”或“添加到词典”等按钮。

在这里'ignore'是上面声明的JButton。

我正在尝试为此方法编写一个测试 - >

 public class SpellCheckerDialog extends JDialog implements ActionListener {

 ...
 ..

public void actionPerformed( ActionEvent ev ) {
    Object source = ev.getSource();
    if( source == ignore ) {
        searchNext();

    }
  }
 ...
}

这是它正在调用的内容,我正在测试是否正在调用此方法。

...

//inside same class
public boolean searchNext() {
    String wordStr;
    while( true ) {
        wordStr = tok.nextInvalidWord();
        if( wordStr == null ) {
            dispose();
            String title = SpellChecker.getApplicationName();
            if(title == null){
                title = this.getTitle();
            }
            SpellChecker.getMessageHandler().handleInformation( getParent(), title, Utils.getResource( "msgFinish" ) );
            return false;
        }
        if( ignoreWords.contains( wordStr ) ) {
            continue;
        }
        String changeTo = changeWords.get( wordStr );
        if( changeTo != null ) {
            replaceWord( wordStr, changeTo );
            continue;
        }
        break;
    }
    word.setText( wordStr );
    notFound.setText( wordStr );

    List<Suggestion> list = dictionary.searchSuggestions( wordStr );

    boolean needCapitalization = tok.isFirstWordInSentence() && Utils.isFirstCapitalized( wordStr );

    Vector<String> suggestionsVector = new Vector<String>();
    for( int i = 0; i < list.size() && i < options.getSuggestionsLimitDialog(); i++ ) {
        Suggestion sugestion = list.get( i );
        String newWord = sugestion.getWord();
        if( needCapitalization ) {
            newWord = Utils.getCapitalized( newWord );
        }
        if( i == 0 )
            word.setText( newWord );
        suggestionsVector.add( newWord );
    }
    suggestionsList.setListData( suggestionsVector );

    addToDic.setEnabled( true );
    return true;
}

到目前为止我尝试过,尝试使用Mockito并调用验证方法,但是这段代码片段似乎不起作用或者有很多依赖,我正在努力解决这个问题。

在我的TestClass中,我有这个 - &gt;

    Dialog fr = Mockito.mock(Dialog.class);
    SpellCheckerDialog sD = new SpellCheckerDialog(fr);
    sD.searchNext();

    Mockito.verify(sD, Mockito.times(1)).thenReturn(searchNext());

我不知道我是否应该为我的(ActionEvent ev)或...

制作存根

1 个答案:

答案 0 :(得分:1)

必须对Mockito创建的模拟进行验证,因为框架不可能知道它不管理的对象会发生什么。这就是说,您的searchNext()方法是受测试类的一部分,因此您可能希望spy on it就像下面的示例中所示:

public class SpyTest {

    class MyClass {

        public void callDoSomething(){
            doSomething();
        }

        public void doSomething(){
            // whatever
        }

    }

    @Test
    public void shouldSpyAndVerifyMethodCall(){
         MyClass objectUnderTest = new MyClass();
         MyClass spy = Mockito.spy(objectUnderTest);

        spy.callDoSomething();

        Mockito.verify(spy, Mockito.times(1)).doSomething();
    }
}

我的建议是通过link above中的Mockito文档和示例,因为它们非常简单,应该给你一个很好的起点。


编辑根据您的评论:

public class SpyTest {
    class MyClass {
        private JButton myButtton;

        public void actionPerformed(ActionEvent event){
            if(event.getSource() == myButtton) {
                searchNext();
            }
        }

        public void searchNext(){
            // whatever
        }
    }

    @Mock // define a mock for the button to "hack" the source check
    private JButton mockButton;

    @InjectMocks // inject the mock in our object under test
    private MyClass objectUnderTest;

    @Test
    public void shouldSpyAndVerifyMethodCall(){
        // spy on our object so we can query various interactions
        MyClass spy = spy(objectUnderTest);

        // event mock
        ActionEvent mockEvent = mock(ActionEvent.class);

        // "hack" the source check
        when(mockEvent.getSource()).thenReturn(mockButton);

        // call main logic
        spy.actionPerformed(mockEvent);


        // verify interactions
        verify(spy).searchNext();  // times(1) not needed because it's the implicit/default setting, see David's comment
    }
}