Menu

#36 Allow expectLastCall().times(0);

open
EasyMock (33)
5
2012-10-05
2010-06-13
Anonymous
No

I use nice mocks all the time in order to have very clean tests, testing usually a single functionality per test. A lot of times I find myself wanting to make sure that a method has not been called on a mock, but this is not possible with niceMocks, since they sort of ignore methods without expectations. In order to support this testing methodology, it is imperative that 0 be a legal amount of times to specify on a method. The only current alternative is to use strict mocks which is very painful to use due to ridiculously large amount of setup if there is a lot of interaction with the mock around the tested interaction (essentially replicating the entire method in each test).

example:

public void setOneOrTheOtherButNotBoth(Long first, Long second){
if (first != null){
setFirst(first);
} else {
setSecond(second);
}
}

@Test
public void testSecondIsNotSetIfFirstIsNotNull() {
niceMockOfTestedObject.setSecond(anyObject());
expectLastCall().times(0);

replay(niceMockOfTestedObject);

mainTestedObject.setOneOrTheOtherButNotBoth(13L, 2L);
verify(niceMockOfTestedObject);
}

Discussion

  • Anonymous

    Anonymous - 2010-06-13

    I wanted to udpate the example to remove any confusion:

    public void setOneOrTheOtherButNotBoth(Long first, Long second){
    if (first != null){
    someObject.setFirst(first);
    } else {
    someObject.setSecond(second);
    }
    }

    @Test
    public void testSecondIsNotSetIfFirstIsNotNull() {
    niceMockOfSomeObject.setSecond(anyObject());
    expectLastCall().times(0);

    replay(niceMockOfSomeObject);

    mainTestedObject.setOneOrTheOtherButNotBoth(13L, 2L);
    verify(niceMockOfSomeObject);
    }

     

    Last edit: Anonymous 2013-11-26

Log in to post a comment.