Reputation: 4666
Is this possible?
I tried with EasyMock.expectLastCall().times(0);
but EasyMock complains that times must be >=1
Upvotes: 23
Views: 19858
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 NiceMock
s, where default values are returned when calling for not recorded methods.
So a solution can be not to use NiceMock
s.
Upvotes: 8
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
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
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
Reputation: 4666
I managed to come up with a solution:
expectLastCall().andAnswer(new IAnswer() {
public Object answer() {
Assert.assertFail();
return null;
}
});
Upvotes: 0
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