Need help writing a test

I am trying to write a test for this class 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 : a receiver is an instance of the Receiver class (the real one is not a mock), a processor is an instance of the Processor class (real not mock) that processes a person (a model of an object of the People class). GetId is a String not int method that is not an error.

Test fails: unexpected call to person.getId ()

I am using jMock, any help would be appreciated. As I understand it, when I call this get method to execute it correctly, I need to make fun of person.getId() , and I have been spinning around in circles for a while, any help would be appreciated.

+7
java unit-testing junit testing jmock
source share
2 answers

If I understand correctly, you need to move the line receiver.get (person); after the context.checking area - because it is the execution of your test and not the setting of expectations. So let this go:

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

In addition, you should use allow () instead of atLeast (1), since here you are truncating the person object. Finally, if Person is just a value type, it is best to use a class.

+1
source share

All Articles