编写测试需要帮助

时间:2010-05-26 11:35:37

标签: java unit-testing testing junit 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!");   
              }
        }

以下是测试:

@Test
    public void testReceivePerson(){
        context.checking(new Expectations() {{          
            receiver.get(person);
            atLeast(1).of(person).getId();
            will(returnValue(String.class));        
        }});
    }

注意:receiver是Receiver类的实例(真的不是mock),processor是处理person(People类的模拟对象)的Processor类(真实不是mock)的实例。 GetId是一个String not int方法,不是错误的。

  

测试失败:意外调用   person.getId()

我正在使用jMock任何帮助将不胜感激。据我所知,当我调用这个get方法来正确执行它时,我需要模仿person.getId(),而且我已经在圈子里徘徊了一段时间,现在任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:4)

如果我理解正确,你必须移动line receiver.get(person);在context.checking的范围之后 - 因为这是测试的执行而不是设置期望。所以给这个人一个:

@Test
public void testReceivePerson(){
    context.checking(new Expectations() {{          
        atLeast(1).of(person).getId();
        will(returnValue(String.class));        
    }});
    receiver.get(person);
}

答案 1 :(得分:1)

此外,您应该使用allow()而不是atLeast(1),因为您在此处存在person对象。最后,如果Person只是一个值类型,那么最好只使用该类。

相关问题