开发者

Need help with writing test

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.


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:

@Test
public void testReceivePerson(){
    context.checking(new Expectations() {{          
        atLeast(1).of(person).getId();
        will(returnValue(String.class));        
    }});
    receiver.get(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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜