ModelAndView返回null SpringBootTest

时间:2018-10-04 11:41:53

标签: java spring-boot junit spring-boot-test

我正在尝试使用Spring Boot学习junit测试。 通常我不会问空指针异常,但现在我找不到丢失的内容。

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
@ActiveProfiles("dev")
@AutoConfigureMockMvc
@WithMockUser(username = "user", password = "secret", authorities = "USER")
public class OwnersWebMVCTests {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testOwners() throws Exception {
        MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/owners");
        ResultActions resultActions = mockMvc.perform(requestBuilder);

        MvcResult mvcResult = resultActions.andReturn();

        ModelAndView mav = mvcResult.getModelAndView();

        MatcherAssert.assertThat(mav.getViewName(), Matchers.equalTo("owners"));
        MatcherAssert.assertThat(mav.getModel().containsKey("owners"), Matchers.is(true));

    }
}

ModelAndView mav = mvcResult.getModelAndView();返回null。

这是我的错误堆栈。

{
java.lang.NullPointerException
    at demo.petClinic.web.OwnersWebMVCTests.testOwners(OwnersWebMVCTests.java:40)
    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:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
    at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
}

有什么可以帮助您的

1 个答案:

答案 0 :(得分:0)

问题与控制器有关,与测试无关。

可能的问题:未扫描您的控制器!我说这是因为您希望拥有相同的GET URL并查看“所有者” =>这将失败,因为它将重定向到自身。

解决方案:请确保控制器和spring boot测试具有相同的软件包名称,或导入配置以对其进行扫描。

OwnerController-> src / main / java / org.test

TestOwnerController-> src / test / java / org.test

相同的软件包名称: org.test

如果需要使用其他软件包名称,则可以添加到测试中

@ComponentScan("org.owner") 

->其中org.owner是OwnerController的软件包

改进:您可以将测试方法重写为

mockMvc.perform(get("/owners"))
       .andExpect(model().attributeExists("owners"))
       .andExpect(view().name("view"));

还请更改返回视图的名称,以确保不会失败:

@GetMapping(value = "/owners")
public ModelAndView getOwnersView() {
    return new ModelAndView("view", Collections.singletonMap("owners", new Object()));
}