MockMvc peform 在集成测试中是 return nullPointerException

MockMvc peform is return nullPointerException in integration tests

我正在尝试 运行 为我的 Spring 项目进行集成测试,这是一个简单的获取方法,returns 从数据库输出给定 ID 的字符串。但是我在测试中 MockMvc.perform 中的 Mockmvc 上不断收到 NullPointerException。

这是测试:

@WebMvcTest(OutputController.class)
public class OutputControllerTests {

    @Autowired
    private MockMvc mockMvc;

    @MockBea
    OutputService outputService;

    @Test
    public void returnOutputForValidId() throws Exception {
        OutputService service = Mockito.mock(OutputService.class);
        when(service.findOutputById("TEST")).thenReturn("Test output");

        String getOutputMapping = "/output/{systemID}";
        String id = "TEST";

        mockMvc.perform(get(getOuputMapping, id))
                .andDo(print()).andExpect(status().isOk())
                .andExpect(content().string("Test output"));
    }

这是控制器 - OutputController:

@RestController
public class OutputController {

    private final OutputService outputService;

    public OutputController(OutputService outputService) {
        this.outputService = outputService;
    }


    @GetMapping("/output/{id}")
    @CrossOrigin
    public String getOutputByID(@PathVariable String Id) {


        String output = outputService.findOutputById(Id);

        return output;
    }
}

完整的错误是:

java.lang.NullPointerException
    at .com.output.OutputControllerTests.returnOutputForValidId(OutputControllerTests.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access0(ParentRunner.java:66)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

您不需要自动装配 MockMvc 对象。试试这个:

import org.junit.jupiter.api.BeforeEach;
//...
private MockMvc mockMvc;
//...

@BeforeEach
public void setUp(WebApplicationContext context) {
   mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}