During suite tests EasyMock says 0 matchers expected 1 recorded

前端 未结 6 1054
走了就别回头了
走了就别回头了 2021-02-19 00:42

So I\'ve been using EasyMock\'s class extension for a while now. All of a sudden I\'m getting this exception, but only when I run the entire test suite:

java.lan         


        
6条回答
  •  北恋
    北恋 (楼主)
    2021-02-19 01:22

    While it's true that this can be a spurious message resulting from a "silly" EasyMock bug, it is also very likely to be due to invalid usage of the EasyMock API. In my case the message arose from this JUnit 3.8 test (and like you, this only happened when I ran my entire suite of tests, and only via Maven, not Eclipse):

    public void testSomething() {
        // Set up
        MyArgumentType mockArg = (MyArgumentType) EasyMock.anyObject(); // bad API usage
    
        // Invoke the method under test
        final String result = objectUnderTest.doSomething(mockArg);
    
        // Verify (assertions, etc.)
        ...
    }
    

    Instead of using anyObject(), I should have used createMock(MyArgumentType.class) or one of its variants. I don't know what I was thinking, I've written millions of these tests and used the API correctly.

    The confusing bit is that the test that fails with the "wrong number of matchers" message isn't necessarily (or ever?) the one in which you used the API wrongly. It might be the first test executed after the buggy one that contains a replay() or verify() method, but I haven't verified this experimentally.

提交回复
热议问题