Java 使用 Mockito 验证在方法之后没有调用任何内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/512254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use Mockito to verify that nothing is called after a method
提问by Moss Collum
I'm using Mockito to write a unit test in Java, and I'd like to verify that a certain method is the last onecalled on an object.
我正在使用 Mockito 在 Java 中编写单元测试,我想验证某个方法是否是对对象调用的最后一个方法。
I'm doing something like this in the code under test:
我正在测试的代码中做这样的事情:
row.setSomething(value);
row.setSomethingElse(anotherValue);
row.editABunchMoreStuff();
row.saveToDatabase();
In my mock, I don't care about the order in which I edit everything on the row, but it's very important that I nottry to do anything more to it after I've saved it. Is there a good way to do this?
在我的模拟中,我不关心我编辑行中所有内容的顺序,但重要的是我在保存后不要尝试对其进行任何操作。有没有好的方法可以做到这一点?
Note that I'm not looking for verifyNoMoreInteractions: it doesn't confirm that saveToDatabase is the last thing called, and it also fails if I call anything on the row that I don't explicitly verify. I'd like to be able to say something like:
请注意,我不是在寻找 verifyNoMoreInteractions:它不能确认 saveToDatabase 是最后一个调用的东西,如果我在没有明确验证的行上调用任何东西,它也会失败。我希望能够这样说:
verify(row).setSomething(value);
verify(row).setSomethingElse(anotherValue);
verifyTheLastThingCalledOn(row).saveToDatabase();
If it helps, I'm switching to Mockito from a JMock test that did this:
如果有帮助,我将从执行此操作的 JMock 测试切换到 Mockito:
row.expects(once()).method("saveToDatabase").id("save");
row.expects(never()).method(ANYTHING).after("save");
采纳答案by Kent Lai
I think it requires more custom work.
我认为它需要更多的定制工作。
verify(row, new LastCall()).saveToDatabase();
and then
进而
public class LastCall implements VerificationMode {
public void verify(VerificationData data) {
List<Invocation> invocations = data.getAllInvocations();
InvocationMatcher matcher = data.getWanted();
Invocation invocation = invocations.get(invocations.size() - 1);
if (!matcher.matches(invocation)) throw new MockitoException("...");
}
}
Previous Answer:
上一个答案:
You are right. verifyNoMoreInteractions is what you need.
你是对的。verifyNoMoreInteractions 正是您所需要的。
verify(row).setSomething(value);
verify(row).setSomethingElse(anotherValue);
verify(row).editABunchMoreStuff();
verify(row).saveToDatabase();
verifyNoMoreInteractions(row);
回答by Rogério
This question led me to make some enhancements to the VerificationsAPI in JMockit(available in the upcoming release 0.983).
这个问题促使我对JMockit 中的验证API (在即将发布的 0.983 版中可用)进行了一些增强。
The solution I came up with allows you to write (in a test method):
我想出的解决方案允许您编写(在测试方法中):
new VerificationsInOrder() {{
unverifiedInvocations();
row.saveToDababase();
}};
... if you only want to verify that a certain method is called after everything else. To verify it happens beforeall other invocations, simply move the call to the top. This actually applies to any sequence of consecutive invocations.
...如果您只想验证在其他所有方法之后调用某个方法。要在所有其他调用之前验证它是否发生,只需将调用移到顶部即可。这实际上适用于任何连续调用序列。
If in addition to the above verification, you also want to verify that some other methods are called in any order, a second verificationsblock can be added to the test (before or after the other block, it doesn't matter):
如果除了上面的验证之外,还想验证其他一些方法是否以任意顺序调用,可以在测试中添加第二个验证块(在其他块之前或之后,没关系):
new Verifications() {{
row.setSomething(value);
row.setSomethingElse(anotherValue);
}};
Although a bit long because of the use of anonymous inner classes, this syntax is both simple and flexible; notice how it adds structure to the test and avoids the repetition of method calls (like verify(...)
). There is more to it than I described here (Hamcrest matchers, invocation counts, etc.), and it's not limited to the verification of instance methods (static methods and constructors can be mocked and verified in the same way).
虽然因为使用匿名内部类有点长,但是这种语法既简单又灵活;注意它如何向测试添加结构并避免方法调用的重复(如verify(...)
)。它比我在这里描述的更多(Hamcrest 匹配器、调用计数等),并且它不仅限于实例方法的验证(静态方法和构造函数可以以相同的方式进行模拟和验证)。
回答by Grofit
Not 100% on topic but I was just looking to find the opposite of verify, and this was the only relevant result, it ends up I was after Mockito.verifyZeroInteractions(mock);
不是 100% 的主题,但我只是想找到验证的反面,这是唯一相关的结果,最终我在 Mockito.verifyZeroInteractions(mock); 之后;
Just incase anyone else ends up here looking for this...
以防万一其他人最终在这里寻找这个......