I have a method which receive a message of from a queue and I need to write the unit test for this method with Mockito
. The method uses a utility class UtilClass
. I am confused about how to write the unit test for this method
public boolean findSomeRecord(Message<?> message){
Details details = UtilClass.getHeaderValue(Constants.DETAILS, message, Details.class);
Record record = recordService.findById(details.getDetailsId());
if(record == null ){
return false;
}
return true;
}
The answer: it depends.
I guess your first problem is that this static method call UtilClass.getHeaderValue()
doesn't function correctly in your unit test?
You see, because if it would function ... what would you care about it? Meaning: you want to test that findSomeRecord()
returns either true or false. So, in a perfect setup, you would only have two tests like:
assertThat(objectUnderTest.findSomeRecord(someRecordThatCanBeFound), is(true));
resp.
assertThat(objectUnderTest.findSomeRecord(someRecordThatCanNotBeFound), is(false));
But probably that static method doesn't work that way in your unit test setup. Then you have two choices: