单元测试 - 使用 MockMvc 测试带有 HashMap 的 @RequestHeader 的控制器
Unit Test - Using MockMvc to test the controller with @RequestHeader with HashMap
我有一个具有以下结构的控制器:
@RequestMapping(value="/validate", method=RequestMethod.POST)
public ResponseEntity<Jwt> IsValid(@RequestBody UserRequest userRequest, @RequestHeader Map<String, String> header) throws Exception {
if(header.contains("key") && header.contains("secret") {
process(header.get("key"), header.get("secret"));
...
...
}
}
我正在使用 MockMvc 为该控制器编写单元测试:
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest
public class AuthenticatorServiceTest {
@Autowired
private MockMvc mockMvc;
@Test
public void CorrectKeyValidation throws Exception {
String key = "correct-key";
String secret = "correct-secret";
Map<String, String> map = new HashMap<>();
map.put("key", key);
map.put("secret", secret);
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/validate")
.header() // <- doesn't accept Map here.
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn();
...
...
}
}
由于 MockMvcRequestBuilders
的 header
方法只接受字符串,我不太确定如何使用 MockMvc 来测试我的控制器 Map<String, String>
作为 @RequestHeader
。任何帮助将不胜感激!
还有如下方法(.headers(*)),你可以用,刚刚验证。
Map<String, String> map = new HashMap<>();
map.put("key", key);
map.put("secret", secret);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(map);
MvcResult result = mvc.perform(MockMvcRequestBuilders
.post("/scheduler/enable")
.headers(httpHeaders)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(this.getRequstAsJSON()))
.andDo(print())
.andReturn();
希望这有用。
我有一个具有以下结构的控制器:
@RequestMapping(value="/validate", method=RequestMethod.POST)
public ResponseEntity<Jwt> IsValid(@RequestBody UserRequest userRequest, @RequestHeader Map<String, String> header) throws Exception {
if(header.contains("key") && header.contains("secret") {
process(header.get("key"), header.get("secret"));
...
...
}
}
我正在使用 MockMvc 为该控制器编写单元测试:
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest
public class AuthenticatorServiceTest {
@Autowired
private MockMvc mockMvc;
@Test
public void CorrectKeyValidation throws Exception {
String key = "correct-key";
String secret = "correct-secret";
Map<String, String> map = new HashMap<>();
map.put("key", key);
map.put("secret", secret);
MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post("/validate")
.header() // <- doesn't accept Map here.
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn();
...
...
}
}
由于 MockMvcRequestBuilders
的 header
方法只接受字符串,我不太确定如何使用 MockMvc 来测试我的控制器 Map<String, String>
作为 @RequestHeader
。任何帮助将不胜感激!
还有如下方法(.headers(*)),你可以用,刚刚验证。
Map<String, String> map = new HashMap<>();
map.put("key", key);
map.put("secret", secret);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setAll(map);
MvcResult result = mvc.perform(MockMvcRequestBuilders
.post("/scheduler/enable")
.headers(httpHeaders)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(this.getRequstAsJSON()))
.andDo(print())
.andReturn();
希望这有用。