During suite tests EasyMock says 0 matchers expected 1 recorded

前端 未结 6 1053
走了就别回头了
走了就别回头了 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 00:59

    I had the same error message. I was (accidentally) using an isA() declaration in the call on the class under test

    I.e.

    classUnderTest.callStateChanged(calls, isA(LoggingOnlyListener.class));
    

    when I meant:

    classUnderTest.callStateChanged(calls, new LoggingOnlyListener());
    

    And it was the test AFTER this one that failed every time.

    0 讨论(0)
  • 2021-02-19 00:59

    I am running into a similar problem. From what I observed, even method returns are matched using Matchers. So if your first method fails for any reason, the matcher for the return match is still in the stack. That could be one reason why you are seeing 1 matchers recorded even when your method doesn't take any argument. Basically, the first method invocation never returned a value.

    0 讨论(0)
  • 2021-02-19 01:05

    I just ran into this problem, and I think I managed to figure it out. For me it was due the the previous test (that's in a different Class), where I was (incorrectly) using an EasyMock matcher in an Assert.assertEquals method.

    It seems EasyMock couldn't complain about the extra matcher reported until the first expects methods was called.

    0 讨论(0)
  • 2021-02-19 01:12

    Which version of Easymock are you using?
    I read a post about the release of v.2.5.2 and previuous versions might have a

    dumb bug on the capture

    try to use Easymock 2.5.2+

    0 讨论(0)
  • 2021-02-19 01:19

    I was sick and tired of seeing this with each new legacy code base with EasyMock I had to work with. Write one new EasyMock test by the book and all of the sudden random tests start failing because of Matchers never captured. So I went looking how EasyMock stores those Matchers. It makes use of a final class LastControl, in that class are a few threadlocals where different things get stored. One of those was for the Matchers. Luck has it that there is a static method on there to pull all the Matchers from the threadlocal that where still on there. So this gave me this idea (with help of a collegue, thanks Sven, he wanted credit)

    /**
     * Base class to make sure all EasyMock matchers are cleaned up.  This is not pretty but it will work
     * 
     * @author N069261KDS
     *
     */
    public class BaseTest {
    
      @Before
      public void before(){
        LastControl.pullMatchers();
      }
    
      @After
      public void after(){
        LastControl.pullMatchers();
      }
    
    }
    

    Basicly let your test that fail with the Matchers error extend from this class and you'll be sure the Matchers are cleaned. Note this IS A WORKAROUND. The offending tests should have been written right in the first place. But if you have to wade through 5000+ tests , this is the lesser of two evils. I hope this will help people out !

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题