I want to test how often a method was called. Im using EasyMock. This is what I have atm.
@TestSubject
Account account = new Account("acc");
@Mock
CheckoutMethod checkoutMethod;
@Mock
CheckoutMethod checkoutMethod2;
@Test
void test_account () {
List<CheckoutMethod> methods = new ArrayList<>();
Item item1 = new Item(1.);
expect(checkoutMethod.pay(item1)).andReturn(true);
expect(checkoutMethod2.pay(item1)).andReturn(true);
replay(checkoutMethod);
replay(checkoutMethod2);
methods.add(checkoutMethod);
methods.add(checkoutMethod2);
account.setCheckoutMethods(methods);
boolean should_work = account.checkoutFor(item1);
Assertions.assertTrue(should_work);
verify(checkoutMethod, expectLastCall().times(1));
verify(checkoutMehtod2, expectLastcall().times(0));
}
the last part is critical and what I want to check.
Im using verify(checkoutMethod, expectLastCall().times(1)); to check if the method is called once and verify(checkoutMehtod2, expectLastCall().times(0)); to verify that the method hasn't been called once.
Can this be done this way?
This is not mockito, it's EasyMock. EasyMock will require a recording and the
verify()will verify all the expectations at once.So, in your case, not called means not recorded. So you test becomes this: