views:

120

answers:

2

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.

+4  A: 

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);
}
Grzenio
@Grzenio thank you, same deal with this as well . I actually solved a problem, I'll post answer now, I made several mistakes.
London
+1  A: 

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.

Steve Freeman