Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 835
London Avatar asked May 26 '10 11:05

London


1 Answers

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);
}
like image 180
Grzenio Avatar answered Sep 30 '22 11:09

Grzenio