java mockito - 检查条件是否通过并调用不返回任何内容的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17765296/
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
mockito - checking if condition passes and calls a method which does not return anythig
提问by Kevin Rave
I have the following service class:
我有以下服务类:
class Person() {
AgeDAO dao;
Speaker speaker;
public method checkSpeak( List<Jon> list) {
List<Integer> ages = dao.getAge(list);
if ( ages != null && !ages.isEmpty() ) {
speaker.speak(list);
}
}
}
Test class:
测试类:
@Test
class MyTest {
void speakTest() {
Person p = new Person();
Speaker speaker = mock(Speaker.class);
p.speaker = speaker;
AgeDAO dao = mock(AgeDAO.class);
p.dao = dao;
List<Jon> list = createJonList();
List<Integer> ages = createAgesList();
when(dao.getAge(anyList())).thenReturn(ages);
p.checkSpeak(ages);
verify(p.speaker).speak(anyList()); // This is not called/invoked.
}
}
I just want to verify speak
is invoked. Should I still mock the method? If I need to mock the method, how do I mock the method that does not return anything. when
requires a method return something.
我只想验证是否speak
被调用。我还应该嘲笑这个方法吗?如果我需要模拟该方法,我该如何模拟不返回任何内容的方法。when
需要一个方法返回一些东西。
回答by Itay Maman
There were a few minor problems in the code you posted (most notably: checkSepak
takes a List<Jon>
but your code is passing ages
which is a List<Integer>
).
您发布的代码中存在一些小问题(最明显的是:checkSepak
需要 aList<Jon>
但您的代码正在通过ages
它是 a List<Integer>
)。
After fixing these I realized that the test is failing because the ages
list, created by the test (and used as the return value of dao.getAges()
), is empty. Thus, the condition ages != null && !ages.isEmpty()
is not satisfied which make the code skip over the speaker.speak()
call.
解决这些问题后,我意识到测试失败了,因为测试ages
创建的列表(并用作 的返回值dao.getAges()
)是空的。因此,ages != null && !ages.isEmpty()
不满足使代码跳过speaker.speak()
调用的条件。
The obvious solution is to make the ages
list non-empty by adding some element to it:
显而易见的解决方案是通过向ages
列表添加一些元素来使列表非空:
public class MyTest {
@Test
public void speakTest() {
Person p = new Person();
Speaker speaker = mock(Speaker.class);
p.speaker = speaker;
AgeDAO dao = mock(AgeDAO.class);
p.dao = dao;
List<Jon> list = createJonList();
List<Integer> ages = createAgesList();
ages.add(5); // Make the list of ages non-empty
when(dao.getAge(anyList())).thenReturn(ages);
p.checkSpeak(list);
verify(p.speaker).speak(anyList());
}
private List<Integer> createAgesList() {
return new ArrayList<Integer>();
}
private List<Jon> createJonList() {
return new ArrayList<Jon>();
}
}
class Person {
AgeDAO dao;
Speaker speaker;
public void checkSpeak(List<Jon> list) {
List<Integer> ages = dao.getAge(list);
if (ages != null && !ages.isEmpty()) {
speaker.speak(list);
}
}
}
回答by mtsui
I don't think you need to mock the object for Speaker since you only used "when" for dao. Unless Speaker has objects that needs to be mocked that would cause the speak method to fail, you should not have to mock Speaker. Try doing p.speaker = new Speaker(); This is the best guess since I'm not sure what Speaker class code looks like.
我认为您不需要为 Speaker 模拟对象,因为您只对 dao 使用了“when”。除非Speaker 有需要模拟的对象会导致speak 方法失败,否则您不应该模拟Speaker。尝试做 p.speaker = new Speaker(); 这是最好的猜测,因为我不确定 Speaker 类代码是什么样的。