Issue
I am seeing inconsistent behaviour in EasyMock tests that I don't understand.
My first test passes..
public class MockATest {
private final AtomicLong aMock = createStrictMock(AtomicLong.class);
@Before
public void setUp() {
aMock.set(101L);
}
@After
public void tearDown() {
aMock.set(999L);
}
@Test
public void testA() {
reset(aMock);
replay(aMock);
// TODO : test stuff here
verify(aMock);
}
}
.. but my second test fails ...
public class MockBTest {
private final List<Long> bMock = createStrictMock(List.class);
@Before
public void setUp() {
bMock.add(101L);
}
@After
public void tearDown() {
bMock.add(999L);
}
@Test
public void testB() {
reset(bMock);
replay(bMock);
// TODO : test stuff here
verify(bMock);
}
}
The failure reason is
Unexpected method call List.add(999)
I have 2 questions really...
- Why is the behaviour different for the 2 tests?
- Why is the add(999L) that happens in the tearDown method is being verified after the verification in the testB method has already fully completed?
(I know I can make this work by adding another reset(bMock) in after the verify(bMock) but I am not sure whether this is just avoiding the issue)
Solution
- Why is the behaviour different for the 2 tests?
Because AtomicLong.set
is typed void AtomicLong.set(long)
so it's a void method. The recording is fine. However, List.add
is typed boolean List.add(E)
so it's not a void method. The correct way to record a non-void method is to do expect(list.add(101L)).andReturn(true)
.
- Why is the add(999L) that happens in the tearDown method is being verified after the verification in the testB method has already fully completed?
Because it never goes in testB()
. EasyMock throws an error on the call to bMock.add(101L)
in setUp()
so it goes directly to the tearDown
which fail as well and hides to exception from setUp()
.
Answered By - Henri