开发者

Test that void method didn't get called with EasyMock

开发者 https://www.devze.com 2023-01-15 22:44 出处:网络
Is this possible? I tried 开发者_JAVA技巧with EasyMock.expectLastCall().times(0); but EasyMock complains that times must be >=1You could use .andThrow(new AssertionFailedError()).anyTimes(); - this is

Is this possible? I tried 开发者_JAVA技巧with EasyMock.expectLastCall().times(0); but EasyMock complains that times must be >=1


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


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();


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.


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() ?


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.


I managed to come up with a solution:

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

精彩评论

暂无评论...
验证码 换一张
取 消