Test that void method didn't get called with EasyMock

家住魔仙堡 提交于 2019-11-27 05:55:32

问题


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


回答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.




回答2:


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



回答3:


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.




回答4:


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




回答5:


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.




回答6:


I managed to come up with a solution:

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


来源:https://stackoverflow.com/questions/3710717/test-that-void-method-didnt-get-called-with-easymock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!