NullPointerExecption failure when creating unit te

2019-08-29 00:12发布

问题:

I'm very new to Mockito and JUnit. I'm working on creating a test case for "forgot password"-workflow. Below is the code for the controller and the test. The test case fails because of a NullPointerException. I added the stacktrace below. Could anyone help me to fix the code? Thanks!

@RequestMapping(value = "/user/public/forgotPassword", method = RequestMethod.POST)
public ModelAndView sendforgetPasswordLink(@ModelAttribute ForgetPasswordBean forgetPasswordBean,BindingResult result, HttpSession session) {

    BreadCrumbBuilder.addLinktoBreadCrumb(session, new Link(Constants.FORGET_PASSWORD_TITLE, "/user/public/forgotPassword", Constants.GROUP_USER, 0));

    Map<String, String> breadCrumbs = HomePageController.setupInitialBreadCrumbs(session);
    breadCrumbs.put(Constants.FORGET_PASSWORD_TITLE,    "/user/public/forgotPassword"); 
    session.setAttribute(SessionAttributes.BREAD_CRUMBS,breadCrumbs);

    ModelAndView mav = new ModelAndView(); 
    mav.addObject("displayTitle", Constants.FORGET_PASSWORD_TITLE);

    PublicUser user = publicUserService.findPublicUserByEmail(forgetPasswordBean.getEmail().toLowerCase());
    if(user == null) {
        result.reject("email", "An account does not exist for this email.");
        mav.setViewName("publicuser/forgetPassword.jsp");
        return mav;
    }

    String randomId = java.util.UUID.randomUUID().toString();
    user.setTempId(randomId);
    mailService.sendForgetPasswordLink(user);
    publicUserService.savePublicUser(user);
    String msg = "Password reset instructions have been sent to your email.";

    mav.addObject("msg", msg);
    mav.setViewName("message.jsp");
    return mav;
}

This is test I created so far

@Test
public void TestForgetPasswordForNoUserFound() throws Exception {

    final String input_email = "abc@test.com";

    ForgetPasswordBean  forgetPasswordBean = new ForgetPasswordBean();
    forgetPasswordBean.setEmail(input_email);


    when(mockPublicUserService.findPublicUserByEmail(input_email)).thenReturn(null);


    final ModelAndView modelAndView = controller.sendforgetPasswordLink(forgetPasswordBean, mockBindingResult, mockHttpSession);

    verify(mockBindingResult).reject("email", "An account does not exist for this email.");
    assertEquals("publicuser/forgetPassword.jsp", modelAndView.getViewName());
    assertModelAttributeValue(modelAndView, "displayTitle", Constants.FORGET_PASSWORD_TITLE);

}

The trace starts here

java.lang.NullPointerException
at gov.hhs.cdelr.web.PublicUserControllerTest.TestForgetPasswordForNoUserFound(PublicUserControllerTest.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Line 410 is this line:

result.reject("email", "An account does not exist for this email.");

回答1:

As the code of your test does not seem to be complete, i assume you forgot to initialize mockBindingResult for this testcase.

Sadly i can't tell you more as i do not see a setUp method nor the rest of the test class.

A setup method could be helpful, as it will run before every test, in there you should initialize your mockBindingResult.

@Before
public void setUp() {
    // Do Stuff before every test
}