Menu

#33 IllegalStateException: incompatible return value type

EasyMock_CE_2.5
closed
5
2012-10-05
2010-04-23
bibodo
No

The following code leads to an "java.lang.IllegalStateException: incompatible return value type"
in the second expect call.

public static class MockTestGetLong {

public Long getLong() {
  return 1l;
}

}

public void testEasyMock() {
MockTestGetLong mock1 = createMock(MockTestGetLong.class);
expect(mock1.getLong()).andReturn(2l);
expect(mock1.toString()).andReturn("foo");
}

The cause is that the first call (getLong()) sets the return type of the state object for the last call to Long. But the second call (toString()) does not set the return type of the state object thus causing the second call to andReturn fails to fail since it expects a Long but gets a String.

Discussion

  • Henri Tremblay

    Henri Tremblay - 2010-05-01

    This is because toString can't be mocked. So EasyMock in fact is seeng this

    expect(mock1.getLong()).andReturn(2l).andReturn("foo");

    because the second expect is ignored. However, I might be able to throw an error when an expect is called on something wrong.

     

Log in to post a comment.