jeudi 8 août 2019

EasyMock verifying calls to mock in tearDown method after verify finished

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...

  1. Why is the behaviour different for the 2 tests?
  2. 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)

Aucun commentaire:

Enregistrer un commentaire