In a project at work I came across a real annoying bug/feature. I found a sollution however I'm really curious to the why it is happening. It all has to do that the project has some legacy tests written with unitils instead of plain old EasyMock. Consider following two test classes ran in order right behind eachother.
public class Test1{
@TestedObject
private MyService myService;
@InjectIntoByType protected Mock<MyRepository> myRepo;
@Test
public void aTest(){
myService.doSomething();
myRepo.assertNotInvoked().getById(EasyMock.anyLong());
}
}
public class Test2{
private IMocksControl control;
private MyOtherService myService;
private MyOtherRepo myRepo;
public Test2(){
control = EasyMock.createControl();
myRepo = control.createMock(MyOtherRepo.class);
myService = new MyOtherService(myRepo);
}
@Test
public void aTest(){
myRepo.getById(5L);
EasyMock.expectLastCall().andReturn(new MyObject(5L));
control.replay()
MyObject result = myService.doSomething();
control.verify()
Assert.assertEquals(5L,result.getId().longValue);
}
}
When I run both test seperatly they run fine. Both green both working. However if I run them right after eachother the second test fails with java.lang.IllegalStateException: x matchers expected, y recorded error (x and y are ints ofcourse, just since this is a fake example I don't have real numbers).
I "fixed" this by changing the assertNotInvoked line to : myRepo.assertNotInvoked().getById(null);
This is ofcourse not really a fix, I just circumvented the use of matchers and I'm wondering if I just didn't break the legacy test case with it ... Is there anyone with enough EasyMock + Unitils experience that could help me understand this ? My best "guess" is that AssertNotInvoked isn't properly ended with a EasyMock.replay(), EasyMock.verify() block ... but I could be wrong.