这是我将要使用 JUnit 和 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();
}
我的测试
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);
}
运行测试时,我得到了一个NullPointerExeption
指向检查方法的点。知道这是从哪里来的吗?是因为我缺乏期待吗?Student 和 obj 不是可模拟的接口。我是新手。跟踪此类测试错误的最佳方法是什么?