I'm new in JUnit
with Mock
. This is actual working but I notice receiving null value in TEST using Map<String, Object>
The reason for this what if their is condition must not null. Someday I'm going to need this.
What I did was since Map<String, Object>
can't be implemented in headers.setAll()
because it need Map<String, String>
so i convert it.
@RestController
public class TestController {
@GetMapping("/test")
public String testAuth(@RequestParam String name, @RequestHeader Map<String, Object> head) {
try {
String conv = String.valueOf( head.get("head"));
return name + " "+ conv ;
} catch (Exception e) {
return e.getMessage();
}
}
}
Test
@WebMvcTest(controllers = TestController.class)
public class ControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testControllerHeader() throws Exception {
Map<String, Object> headerMap = new HashMap<String, Object>();
Map<String, String> headObjectToString = headerMap.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> (String)e.getValue()));
HttpHeaders headers = new HttpHeaders();
headers.setAll(headObjectToString);
MvcResult result = mockMvc.perform(MockMvcRequestBuilders
.get("/test?name=justin")
.headers(headers)
.accept(MediaType.APPLICATION_JSON)
).andExpect(status().isOk()).andReturn();
//i make it error just to see the body
assertEquals(result.getResponse().getStatus(), 201);
//this the original
//assertEquals(result.getResponse().getStatus(), 200);
}
}
Body = justin null
MockHttpServletRequest:
HTTP Method = GET
Request URI = /test
Parameters = {name=[justin]}
Headers = [Accept:"application/json"]
Body = null
Session Attrs = {}
Handler:
Type = com.mock.TestingMokito.Controller.TestController
Method = com.mock.TestingMokito.Controller.TestController#testAuth(String, Map)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Type:"application/json", Content-Length:"10"]
Content type = application/json
Body = justin null
Forwarded URL = null
Redirected URL = null
Cookies = []
You're passing null in the header, that's why it is returning null. In your test class, add some data in the headerMap before converting it to a string.
@WebMvcTest(controllers = TestController.class)
public class ControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testControllerHeader() throws Exception {
Map<String, Object> headerMap = new HashMap<String, Object>();
headerMap.put("head",new String("This was the bug"));
Map<String, String> headObjectToString = headerMap.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> (String)e.getValue()));
HttpHeaders headers = new HttpHeaders();
headers.setAll(headObjectToString);
MvcResult result = mockMvc.perform(MockMvcRequestBuilders
.get("/test?name=justin")
.headers(headers)
.accept(MediaType.APPLICATION_JSON)
).andExpect(status().isOk()).andReturn();
//i make it error just to see the body
assertEquals(result.getResponse().getStatus(), 201);
//this the original
//assertEquals(result.getResponse().getStatus(), 200);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.