Here is a controller method I'm about to test with JUnit and JMock.
public String myMethod(ModelMap model, Principal principal,
@PathVariable MyObject obj) {
if (obj != null) {
Student student = getStudent(principal);
if (check(obj, student)) {
model.addAttribute(student).addAttribute(obj);
return "page";
}
}
return REDIR;
}
private boolean check(MyObject obj, Student student) {
return student.getStudentNumber().longValue() == obj.getStudent().getStudentNumber().longValue();
}
My test
final StudentService studentService = context.mock(StudentService.class);
public void test() throws Exception {
ModelMap model = new ModelMap();
final MyObject = new myObject();
context.checking(new Expectations() {
{
oneOf(studentService).getByNumber(SecurityHelper.getStudentNumberOf(Helper.getTestPrincipal()));
will(returnValue(mockStudent));
}
});
controller.myMethod(model, Helper.getTestPrincipal(), obj);
}
When running test, I get a NullPointerExeption
which points to check-method. Any idea where that comes from? Is it because I lack some expectation? Student and obj are not interfaces to be mockable. I'm new with this. Which is the best way to track these kind of testing errors?