开发者

More Matchers recorded than the expected - Easymock fails from Maven and not from Eclipse

开发者 https://www.devze.com 2023-02-24 09:27 出处:网络
I\'m having a strange problem with Easymock 3.0 and JUnit 4.8.2. The problem only occurs when executing the tests from Maven and not from Eclipse.

I'm having a strange problem with Easymock 3.0 and JUnit 4.8.2. The problem only occurs when executing the tests from Maven and not from Eclipse.

This is the unit test (very simple):

...
protected ValueExtractorRetriever mockedRetriever;
...

@Before
public void before() {
    mockedRetriever = createStrictMock(ValueExtractorRetriever.class);
}

@After
public void after() {
    reset(mockedRetriever);
}

@Test
public void testNullValueExtractor() { 
    expect(mockedRetriever.retrieve("PROP")).andReturn(null).once();
    replay(mockedRetriever);

    ValueExtractor retriever = mockedRetriever.retrieve("PROP");
    assertNull(retriever);

    assertTrue(true);
}

And I get:

开发者_开发知识库

java.lang.IllegalStateException: 1 matchers expected, 2 recorded.

The weird thing is that I'm not even using an argument matcher. And that is the only method of the test! and to make it even worst it works from Eclipse and fails from Maven!

I found a few links which didn't provide me with an answer:

  • Another StackOverflow post
  • Expected Exceptions in JUnit

If I change the unit test and add one more method (which does use an argument matcher):

@Test
public void testIsBeforeDateOk() {
    expect(mockedRetriever.retrieve((String)anyObject())).andReturn(new PofExtractor()).anyTimes();
    replay(this.mockedRetriever);

    FilterBuilder fb = new FilterBuilder();
    assertNotNull(fb);

    CriteriaFilter cf = new CriteriaFilter();
    assertNotNull(cf);
    cf.getValues().add("2010-12-29T14:45:23");
    cf.setType(CriteriaType.DATE);
    cf.setClause(Clause.IS_BEFORE_THE_DATE);

    CriteriaQueryClause clause = CriteriaQueryClause.fromValue(cf.getClause());
    assertNotNull(clause);
    assertEquals(CriteriaQueryClause.IS_BEFORE_THE_DATE, clause);

    clause.buildFilter(fb, cf, mockedRetriever);
    assertNotNull(fb);

    Filter[] filters = fb.getFilters();
    assertNotNull(filters);
    assertEquals(filters.length, 1);

    verify(mockedRetriever);

    logger.info("OK");
}

this last method passes the test but not the other one. How is this possible!?!?!

Regards, Nico

More links:

"bartling.blogspot.com/2009/11/using-argument-matchers-in-easymock-and.html"

"www.springone2gx.com/blog/scott_leberknight/2008/09/the_n_matchers_expected_m_recorded_problem_in_easymock"

"stackoverflow.com/questions/4605997/3-matchers-expected-4-recorded"


I had a very similar problem and wrote my findings in the link below. http://www.flyingtomoon.com/2011/04/unclosed-record-state-problem-in.html (just updated)

I believe the problem in on another test that affects your current test. The problem is on another test class and it affects you test. In order to find the place of the real problem, I advice to disable the problematic tests one by one till you notify the failing test.

Actually this is what I did. I disabled the failing tests one by one till I found the problematic test. I found a test that throws an exception and catches by "@extected" annotation without stopping the recording.


We had this problem recently, and it only reared its head when we ran the entire test suite (1100+ test cases). Eventually, I found that I could put a breakpoint on the test that was blowing up, and then step back in the list of tests that Eclipse had already executed, looking for the previous test case that had set up a mock incorrectly.

Our problem turned out to be somebody using EasyMock.anyString() outside of an EasyMock.expect(...) statement. Sure enough, it was done two tests before the one that was failing.

So essentially, what was happening is that the misuse of a matcher outside of an expect statement was poisoning EasyMock's state, and the next time we tried to create a mock, EasyMock would blow up.


I believe the first error message

java.lang.IllegalStateException: 1 matchers expected, 2 recorded.

means your mockedRetriever methods called twice but test expects it was called once. So your Eclipse and Maven's configuration differs.

And I have no reason to reset mock after test. Just keep in mind JUnit creates new class instance for every single test method.

EDITED:

What about the reason why the last test method passed the answer is:

expect(mockedRetriever.retrieve((String)anyObject())).andReturn(new PofExtractor()).anyTimes();

But in your first test method it is:

expect(mockedRetriever.retrieve("PROP")).andReturn(null).once();

as equivalent of:

expect(mockedRetriever.retrieve("PROP")).andReturn(null);
0

精彩评论

暂无评论...
验证码 换一张
取 消