Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要写作测试的帮助吗_Java_Unit Testing_Testing_Junit_Jmock - Fatal编程技术网

Java 需要写作测试的帮助吗

Java 需要写作测试的帮助吗,java,unit-testing,testing,junit,jmock,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.in

我正试图为这个类编写一个名为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类(real而非mock)的实例,processor是processor类(real而非mock)的实例,它处理person(People类的mock对象)。GetId是一个字符串而不是int方法,这是正确的

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


我正在使用jMock,如果您能提供任何帮助,我将不胜感激。正如我在调用此
get
方法以正确执行它时所理解的那样,我需要模拟
person.getId()
,并且我已经在一段时间内四处打转了,希望能得到任何帮助。

如果我理解正确,您必须移动线路接收器。get(person);在context.checking的范围之后-因为这是测试的执行,而不是设置期望。所以,试试这个:

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

另外,您应该使用allowing()而不是至少(1),因为您正在这里存根person对象。最后,如果Person只是一个值类型,那么最好只使用该类。

谢谢,同样处理这个问题。我实际上解决了一个问题,我现在就发布答案,我犯了几个错误。