我正在学习制作API。我正在尝试对我的Get请求运行测试,但是在我的测试类中一直得到Null Pointer Exception。我错过了什么?
我正在使用Spring Boot / Maven / Java11。据我所知,这是一个非常基本的设置,因为我刚刚开始涉足Spring Boot。
请参见下面的代码:
控制器测试等级:
package ControllerTest;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
public class StatControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testGetResponse()
throws Exception {
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders
.get("/get");
ResultMatcher contentMatcher = MockMvcResultMatchers.content()
.string("GET Response");
mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk());
}
}
控制器类别:
package apiPackage;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
public class StatController {
@RequestMapping("/")
public String index(){
return "Welcome to the SilverSnacks Stat Sheet!";
}
@GetMapping("/get")
public @ResponseBody ResponseEntity<String> get(){
return new ResponseEntity<String>("GET Response", HttpStatus.OK);
}
@GetMapping("/get/{id}")
public @ResponseBody ResponseEntity<String> getById(@PathVariable String id){
return new ResponseEntity<String>("GET Response:" + id, HttpStatus.OK);
}
@PutMapping("/put")
public @ResponseBody ResponseEntity<String> put(){
return new ResponseEntity<String>("PUT Response", HttpStatus.OK);
}
@PostMapping("/post")
public @ResponseBody ResponseEntity<String> post(){
return new ResponseEntity<String>("POST Response", HttpStatus.OK);
}
@DeleteMapping("/delete")
public @ResponseBody ResponseEntity<String> delete(){
return new ResponseEntity<String>("DELETE Response", HttpStatus.OK);
}
@PatchMapping("/patch")
public @ResponseBody ResponseEntity<String> patch(){
return new ResponseEntity<String>("PATCH Response", HttpStatus.OK);
}
}
错误输出:
java.lang.NullPointerException
at ControllerTest.StatControllerTest.testGetResponse(StatControllerTest.java:26)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
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.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
尝试将以下注释添加到 StatControllerTest
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class StatControllerTest {
@Autowired
private MockMvc mockMvc;
....
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句