预期状态:<200> 但为:<400> MockMvc 错误
Status expected:<200> but was:<400> error with MockMvc
我在测试我的 Spring 引导控制器时遇到了问题,我在过去几个小时里一直在努力解决这个问题。我对编码很陌生,但我还是在公司得到了那个任务。
我的控制器:
@GetMapping(value = "/data", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> getData(@RequestParam("core") List<String> core, @RequestParam("date") List<String> date,
@RequestParam("state") String state, @RequestParam("area") List<String> area,
@RequestParam("type") List<String> type, @RequestParam("analyt") String analyt,
@RequestParam("wp") String wp, @RequestParam("subDesc") String subDesc,
@RequestParam("anaMonitor") List<String> anaMonitor, @RequestParam("ecoMonitor") List<String> ecoMonitor,
@RequestParam("subType") List<String> subType, @RequestParam("needed") List<String> needed,
@RequestParam("notify") List<String> notify, @RequestParam("available") List<String> available,
@RequestParam("phase") List<String> phase, @RequestParam("subAvailable") List<String> subAvailable,
@RequestParam("subShipped") List<String> subShipped, @RequestParam("external") List<String> external,
@RequestParam("valid") List<String> valid, @RequestParam("contract") List<String> contract) {
FilterInputModel model = new FilterInputModel(core, date, state, type, area, analyt, wp, subDesc, anaMonitor, ecoMonitor, subType, needed, notify, available, phase, subAvailable, subShipped, external, valid, contract);
return new ResponseEntity<>(vaultService.getFilteredFields(model), HttpStatus.OK);
}
这里有一个测试:
@DisplayName("Controller Test")
@AutoConfigureMockMvc
@WebMvcTest(controllers = Controller.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
class ControllerTest {
@Autowired
private MockMvc mockMvc;
private FilterInputModel model;
@MockBean
private TestService testService;
@Autowired
ControllerTest() {
}
@BeforeEach
void setup() {
MockitoAnnotations.openMocks(this);
@Test
void getDataTest() throws Exception {
List<Object> response = new ArrayList<>();
when(vaultService.getFilteredFields(model))
.thenReturn(response)
.thenReturn(HttpStatus.OK);
this.mockMvc.perform(
MockMvcRequestBuilders.get("/api/data")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept("application/json"))
.andExpect(MockMvcResultMatchers.status().isOk());
.andExpect(result -> assertEquals(response, result.getAsyncResult()));
}
这是我得到的答案。它在一条错误消息中说“方法参数类型列表的必需请求参数 'core' 不存在”,但这个答案对我帮助不大。我仍然在寻找答案。
MockHttpServletRequest:
HTTP Method = GET
Request URI = /api/data
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json"]
Body = null
Session Attrs = {}
Handler:
Type = hr.ogcs.ltt.api.lttapi.controller.LttController
Method = hr.ogcs.ltt.api.lttapi.controller.LttController#getData(List, List, String, List, List, String, String, String, List, List, List, List, List, List, List, List, List, List, List, List)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = org.springframework.web.bind.MissingServletRequestParameterException
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 400
Error message = Required request parameter 'core' for method parameter type List is not present
Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
Status expected:<200> but was:<400>
Expected :200
Actual :400
<Click to see difference>
java.lang.AssertionError: Status expected:<200> but was:<400>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher(StatusResultMatchers.java:627)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:212)
at hr.ogcs.ltt.api.apicontroller.ControllerTest.getDataTest(ControllerTest.java:126) <31 internal lines>
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)<9 internal lines>
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)<48 internal lines>
您没有向该方法发送所需的参数(用 @RequestParam
注释的参数)。默认情况下,它们都是必需的。如果不存在 Spring 会阻止缺少参数的请求。
发送参数示例:
mvc.perform(
MockMvcRequestBuilders.get("/public/xpto/{code}", "21212").param("core", "ubababacaio")
.contentType(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON_VALUE).characterEncoding("UTF-8")).andExpect(status().isOk());
我在测试我的 Spring 引导控制器时遇到了问题,我在过去几个小时里一直在努力解决这个问题。我对编码很陌生,但我还是在公司得到了那个任务。
我的控制器:
@GetMapping(value = "/data", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> getData(@RequestParam("core") List<String> core, @RequestParam("date") List<String> date,
@RequestParam("state") String state, @RequestParam("area") List<String> area,
@RequestParam("type") List<String> type, @RequestParam("analyt") String analyt,
@RequestParam("wp") String wp, @RequestParam("subDesc") String subDesc,
@RequestParam("anaMonitor") List<String> anaMonitor, @RequestParam("ecoMonitor") List<String> ecoMonitor,
@RequestParam("subType") List<String> subType, @RequestParam("needed") List<String> needed,
@RequestParam("notify") List<String> notify, @RequestParam("available") List<String> available,
@RequestParam("phase") List<String> phase, @RequestParam("subAvailable") List<String> subAvailable,
@RequestParam("subShipped") List<String> subShipped, @RequestParam("external") List<String> external,
@RequestParam("valid") List<String> valid, @RequestParam("contract") List<String> contract) {
FilterInputModel model = new FilterInputModel(core, date, state, type, area, analyt, wp, subDesc, anaMonitor, ecoMonitor, subType, needed, notify, available, phase, subAvailable, subShipped, external, valid, contract);
return new ResponseEntity<>(vaultService.getFilteredFields(model), HttpStatus.OK);
}
这里有一个测试:
@DisplayName("Controller Test")
@AutoConfigureMockMvc
@WebMvcTest(controllers = Controller.class, excludeAutoConfiguration = SecurityAutoConfiguration.class)
class ControllerTest {
@Autowired
private MockMvc mockMvc;
private FilterInputModel model;
@MockBean
private TestService testService;
@Autowired
ControllerTest() {
}
@BeforeEach
void setup() {
MockitoAnnotations.openMocks(this);
@Test
void getDataTest() throws Exception {
List<Object> response = new ArrayList<>();
when(vaultService.getFilteredFields(model))
.thenReturn(response)
.thenReturn(HttpStatus.OK);
this.mockMvc.perform(
MockMvcRequestBuilders.get("/api/data")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept("application/json"))
.andExpect(MockMvcResultMatchers.status().isOk());
.andExpect(result -> assertEquals(response, result.getAsyncResult()));
}
这是我得到的答案。它在一条错误消息中说“方法参数类型列表的必需请求参数 'core' 不存在”,但这个答案对我帮助不大。我仍然在寻找答案。
MockHttpServletRequest:
HTTP Method = GET
Request URI = /api/data
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json"]
Body = null
Session Attrs = {}
Handler:
Type = hr.ogcs.ltt.api.lttapi.controller.LttController
Method = hr.ogcs.ltt.api.lttapi.controller.LttController#getData(List, List, String, List, List, String, String, String, List, List, List, List, List, List, List, List, List, List, List, List)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = org.springframework.web.bind.MissingServletRequestParameterException
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 400
Error message = Required request parameter 'core' for method parameter type List is not present
Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers"]
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
Status expected:<200> but was:<400>
Expected :200
Actual :400
<Click to see difference>
java.lang.AssertionError: Status expected:<200> but was:<400>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:59)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:122)
at org.springframework.test.web.servlet.result.StatusResultMatchers.lambda$matcher(StatusResultMatchers.java:627)
at org.springframework.test.web.servlet.MockMvc.andExpect(MockMvc.java:212)
at hr.ogcs.ltt.api.apicontroller.ControllerTest.getDataTest(ControllerTest.java:126) <31 internal lines>
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)<9 internal lines>
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)<48 internal lines>
您没有向该方法发送所需的参数(用 @RequestParam
注释的参数)。默认情况下,它们都是必需的。如果不存在 Spring 会阻止缺少参数的请求。
发送参数示例:
mvc.perform(
MockMvcRequestBuilders.get("/public/xpto/{code}", "21212").param("core", "ubababacaio")
.contentType(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON_VALUE).characterEncoding("UTF-8")).andExpect(status().isOk());