简体   繁体   English

测试控制器方法时发生NullpointerException

[英]NullpointerException when testing a controller method

Here is a controller method I'm about to test with JUnit and JMock. 这是我将要使用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();
}

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. 运行测试时,我得到一个NullPointerExeption ,它指向检查方法。 Any idea where that comes from? 知道那是哪里来的吗? Is it because I lack some expectation? 是因为我缺乏期望吗? Student and obj are not interfaces to be mockable. Student和obj不是可模拟的接口。 I'm new with this. 我是新来的。 Which is the best way to track these kind of testing errors? 跟踪此类测试错误的最佳方法是哪种?

If i understand you correctly, this is the line that throws an exception: 如果我正确理解您的意见,这是引发异常的行:

return student.getStudentNumber().longValue() == obj.getStudent()
    .getStudentNumber().longValue();

So the exception can come from several places: 因此,异常可能来自几个地方:

  1. studen or obj are null. studenobj为null。
  2. student.getStudentNumber() returns null. student.getStudentNumber()返回null。
  3. obj.getStudent() returns null obj.getStudent()返回null
  4. obj.getStudent().getStudentNumber() returns null. obj.getStudent().getStudentNumber()返回null。

Do a System.out on all of these statements and see which is null. 在所有这些语句上执行System.out,看看哪个为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试Controller时出现Nullpointerexception EasyMock - Nullpointerexception EasyMock when testing Controller 在 SpringBoot API 中测试时 controller 上出现 NullPointerException - Mocking - NullPointerException on controller when testing in SpringBoot API - Mocking MockMvc Controller 测试并返回 NullPointerException - MockMvc Controller Testing and return NullPointerException 使用Mockito测试时,超类方法的构造函数中出现NullPointerException - NullPointerException in super class method's constructor when testing using mockito Java - 初始化数组后和使用类方法测试数组时的 NullPointerException - Java - NullPointerException after initializing array and when testing array with class method 单元测试时出现 NullPointerException 错误 - NullPointerException error when unit testing 调用方法时发生NullPointerException - NullPointerException when calling a method 模拟控制器类时出现NullPointerException - NullPointerException when mocking controller class 测试Web层时模拟控制器类的方法 - Mocking a method of controller class when testing the web layer 并行测试返回java.lang.NullPointerException时调用另一个方法 - Calling another method when Parallel Testing returns java.lang.NullPointerException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM