Lon*_*don 7 java testing junit unit-testing jmock
我正在尝试为这个类编写一个名为Receiver的测试:
public void get(People person) {
if(null != person) {
LOG.info("Person with ID " + person.getId() + " received");
processor.process(person);
}else{
LOG.info("Person not received abort!");
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试:
@Test
public void testReceivePerson(){
context.checking(new Expectations() {{
receiver.get(person);
atLeast(1).of(person).getId();
will(returnValue(String.class));
}});
}
Run Code Online (Sandbox Code Playgroud)
注意:receiver是Receiver类的实例(真的不是mock),processor是处理person(People类的模拟对象)的Processor类(真实不是mock)的实例.GetId是一个String not int方法,不是错误的.
测试失败:person.getId()的意外调用
我正在使用jMock任何帮助将不胜感激.据我所知,当我称这种get方法正确执行时,我需要嘲笑person.getId(),而且我已经在圈子里徘徊了一段时间,现在任何帮助都会受到赞赏.
如果我理解正确,你必须移动 receive.get(person); 行 在 context.checking 的范围之后 - 因为这是测试的执行而不是设置期望。所以尝试一下这个:
@Test
public void testReceivePerson(){
context.checking(new Expectations() {{
atLeast(1).of(person).getId();
will(returnValue(String.class));
}});
receiver.get(person);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
204 次 |
| 最近记录: |