1

我有一个spring mvc应用程序并使用mockito进行单元测试。运行单元测试时,我不断收到空指针异常。:(

请在下面找到我的单元测试将基于的方法:

@Controller
public class LogInController {

    @Autowired
    private MessageSource messageSource;

    @RequestMapping(method = RequestMethod.POST, value = "/login")
    public ModelAndView validateViewLogin(@ModelAttribute Person person,
    BindingResult result, HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();

        String userName = person.getFirstName();
        String password = person.getPassword();
        boolean isUserValid = false;
        if (userName != null && password != null) {
             isUserValid = userManagerService.validateUserLogin(userName,password);
        }
        if (!isUserValid) {
            mav.addObject("failLog",messageSource.getMessage("login.user.fail", new String[] {"  ", "" }, request.getLocale()));
            mav.addObject("isUserValid", false);
            mav.setViewName("login");
            return mav;
        }
        mav.addObject("isUserValid", isUserValid);
        mav.setViewName("home");
        return mav;
    }

请在我的单元测试下面找到:

@Test
public void validateViewLogin_NotValidLogin_returnsLoginPage() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    Person person = new Person();
    person.setFirstName("John");
    person.setPassword("123");
    isUserValid = false;
    LogInController controller = new LogInController();

    // Inject mock user service into controller
    UserManagerService mockUserService =   mock(UserManagerService.class);
    controller.setUserManagerService(mockUserService);

    // Configure mock user service to accept the person
    when(mockUserService.validateUserLogin("John", "123")).thenReturn(
            isUserValid);

    // Attempt the validation
    ModelAndView mav = controller
            .validateViewLogin(person, result, request);

    // Check the result
    assertEquals("login", mav.getViewName());
}

当我运行上面的单元测试时,请在下面找到堆栈跟踪错误消息:

java.lang.NullPointerException
at com.gemstone.presentation.LogInController.validateViewLogin(LogInController.java:99)
at com.gemstone.presentation.LogInControllerTest.validateViewLogin_NotValidLogin_returnsLoginPage(LogInControllerTest.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
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:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

基本上下面显示的第 99 行根据堆栈跟踪返回 null:

mav.addObject(
                "failLog",
                messageSource.getMessage("login.user.fail", new String[] {
                        "  ", "" }, request.getLocale()));

任何想法为什么我在这一行上得到空指针?但是当我运行我的应用程序时它工作正常并且failLog消息显示在我的页面上。

提前致谢。

4

1 回答 1

3

我的猜测是您的 messageSource 为空。您正在模拟您的请求,但不是您的 messageSource,并且您正在新建登录控制器,它没有被 Spring 注入。

在运行时,它会被 Spring 自动注入。

编辑以澄清:

首先,您需要能够设置一些测试方法。您可以添加一个 setter 或一个构造函数来获取它,例如:

@Controller
public class LogInController {

    @Autowired
    private MessageSource messageSource;

    public void setMessageSource(MessageSource ms){
        this.messageSource = ms;
    }
    // ......
}

那么你的测试也需要改变:

    @Test
public void validateViewLogin_NotValidLogin_returnsLoginPage() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    Person person = new Person();
    person.setFirstName("John");
    person.setPassword("123");
    isUserValid = false;
    LogInController controller = new LogInController();

    // Inject mock user service into controller
    UserManagerService mockUserService =   mock(UserManagerService.class);

            /** ADDED **/
            MessageSource ms = mock(MessageSource.class);

    controller.setUserManagerService(mockUserService);
            /** ADDED **/
            controller.setMessageSource(ms);

    // Configure mock user service to accept the person
    when(mockUserService.validateUserLogin("John", "123")).thenReturn(
            isUserValid);

            /** ADDED **/
            when(ms.getMessage("login.user.fail", new String[] {"  ", "" }).thenReturn("message");

    // Attempt the validation
    ModelAndView mav = controller
            .validateViewLogin(person, result, request);

    // Check the result
    assertEquals("login", mav.getViewName());
}

随便放这个,所以请确保我没有任何错别字!本质上,您可以像使用 UserManagerService 一样尝试 MessageSource,它毕竟只是一个接口,并且可以以完全相同的方式进行模拟。

于 2013-01-23T18:44:03.833 回答