nkr1pt
nkr1pt

Reputation: 4666

Test that void method didn't get called with EasyMock

Is this possible? I tried with EasyMock.expectLastCall().times(0); but EasyMock complains that times must be >=1

Upvotes: 23

Views: 19858

Answers (6)

Vic
Vic

Reputation: 1888

The fact that some method is not called is controlled by Mock or StrictMock. They will throw an exception, when that not recorded method is called. This problem occurs only when using NiceMocks, where default values are returned when calling for not recorded methods.

So a solution can be not to use NiceMocks.

Upvotes: 8

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79875

You could use .andThrow(new AssertionFailedError()).anyTimes(); - this is the same exception that Assert.fail() throws, but is less verbose than making an Answer.

Upvotes: 27

David Nguyen
David Nguyen

Reputation: 221

with easymock 3.0, you need to add a .anyTimes() on the expectLastCall or the test will fail:

Expectation failure on verify: myMethod(): expected: 1, actual: 0`

based on nkr1pt example:

expectLastCall().andAnswer(new IAnswer() {
    public Object answer() {
      Assert.assertFail();
      return null;
    }
}).anyTimes();

Upvotes: 10

Henri Tremblay
Henri Tremblay

Reputation: 656

If you expect your method not to be called then just don't record it. But I agree it won't work with a nice mock.

Upvotes: 1

nkr1pt
nkr1pt

Reputation: 4666

I managed to come up with a solution:

expectLastCall().andAnswer(new IAnswer() {
    public Object answer() {
        Assert.assertFail();
        return null;
    }
});

Upvotes: 0

Christian Strempfer
Christian Strempfer

Reputation: 7383

Looks like a bug to me. The internal class Range does not allow to set a maximum less than 1.

Couldn't you mock that method, and just call Assert.fail() ?

Upvotes: 1

Related Questions