需要编写测试帮助
我正在尝试为这个名为 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!");
}
}
这是测试:
@Test
public void testReceivePerson(){
context.checking(new Expectations() {{
receiver.get(person);
atLeast(1).of(person).getId();
will(returnValue(String.class));
}});
}
注意:接收器是 Receiver 类的实例(真实而不是模拟),处理器是 Processor 类的实例(真实的而不是模拟的)处理人(People 类的模拟对象)。 GetId 是一个 String 而不是 int 方法,没有错误。
测试失败:意外调用 person.getId()
我正在使用 jMock 任何帮助将不胜感激。据我了解,当我调用此 get
方法来正确执行它时,我需要模拟 person.getId()
,而且我已经绕了一段时间了任何帮助将不胜感激。
I'm trying to write a test for this class its called 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!");
}
}
Here is the test :
@Test
public void testReceivePerson(){
context.checking(new Expectations() {{
receiver.get(person);
atLeast(1).of(person).getId();
will(returnValue(String.class));
}});
}
Note: receiver is the instance of Receiver class(real not mock), processor is the instance of Processor class(real not mock) which processes the person(mock object of People class). GetId is a String not int method that is not mistake.
Test fails : unexpected invocation of
person.getId()
I'm using jMock any help would be appreciated. As I understood when I call this get
method to execute it properly I need to mock person.getId()
, and I've been sniping around in circles for a while now any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
另外,您应该使用 allowed() 而不是 atLeast(1),因为您在这里存根了 person 对象。最后,如果 Person 只是一个值类型,那么最好只使用该类。
Also, you should use allowing() instead of atLeast(1), since you're stubbing the person object here. Finally, if Person is just a value type, it might be better to just use the class.
如果我理解正确,你必须移动 receive.get(person); 行在 context.checking 的范围之后 - 因为这是测试的执行而不是设置期望。所以尝试一下这个:
If I understand correctly, you have to move the line receiver.get(person); after the scope of context.checking - because this is the execution of your test and not setting expectation. So give this one a go: