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

easymockeclipsejavajunitmaven

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:

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"

Best Answer

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.