我試圖為我的Spring專案運行集成測驗,它是一個簡單的get方法,從DB中為給定的id回傳一個字串輸出。但是我在測驗中的Mockmvc.execute中一直得到一個NullPointerException。
這里是測驗:
@WebMvcTest(OutputController.class)
public class OutputControllerTests {
@Autowired[/span
private MockMvc mockMvc;
@MockBea
輸出服務outputService。
@Test
public void returnOutputForValidId() throws Exception {
OutputService service = Mockito.mock(OutputService.class)。
當(service.findOutputById("TEST")).thenReturn("Test output")。
String getOutputMapping = "/output/{systemID}"。
String id = "TEST" ;
mockMvc.former(get(getOuputMapping, id))
.andDo(print()).andExpect(status().isOk())
.andExpect(content().string("測驗輸出"))。
}
這里是控制器 - OutputController:
@RestController
public class OutputController {
private finally OutputService outputService;
public OutputController(OutputService outputService) {
this.outputService = outputService。
}
@GetMapping("/output/{id}"/span>)
@CrossOrigin(span class="hljs-string">"/output/{id}")
public String getOutputByID(@PathVariable String Id) {
String output = outputService.findOutputById(Id)。
return output。
}
完整的錯誤是 :
java.lang.NullPointerException。
在.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)。
在java.lang.reflect.Method.invoke(Method.java:498)。
在org.junit.runners.model。 FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
在org.junit.internal.runners。 model.ReflectiveCallable.run(ReflectiveCallable.java:12)。
在org.junit.runners.model。 FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
在org.junit.internal.runners。 statements.InvokeMethod.evaluation(InvokeMethod.java:17)
在org.junit.runners。 ParentRunner$3.evaluation(ParentRunner.java:306)
at org.junit.runners. BlockJUnit4ClassRunner$1.evaluation(BlockJUnit4ClassRunner.java:100)
在org.junit.runners。 ParentRunner.runLeaf(ParentRunner.java:366)
at org.junit.runners. BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
在org.junit.runners。 BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
在org.junit.runners。 ParentRunner$4.run(ParentRunner.java:331)
在org.junit.runners。 ParentRunner$1.schedule(ParentRunner.java:79)
在org.junit.runners。 ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)。
在org.junit.runners。 ParentRunner$2.evaluation(ParentRunner.java:293)
在org.junit.runners。 ParentRunner$3.evaluation(ParentRunner.java:306)
at org.junit.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)
uj5u.com熱心網友回復:
你不需要自動連接MockMvc物件。試著用這個來代替:
import org.junit.jupiter.api.BeforeEach;
//...
private MockMvc mockMvc。
//...
@BeforeEach
public void setUp(WebApplicationContext context) {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build()。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/313481.html
標籤:
