Reputation: 1439
I am using junit
for unit testing in spring data jpa
application. I am trying to write junit test cases for controller class unclaimedRoomAssign() . I am using mockito
for creating mock object.
I tried to write test like below but I am getting test fail 400 bad request. can any one tell me what I am missing in test cases
RoomDepartmentMappingController class
public class RoomDepartmentMappingController {
@Autowired
RoomDepartmentMappingService rdmService;
@PostMapping("/assignUnclaimedRooms")
public ResponseEntity<String> unclaimedRoomAssign(@NotNull @RequestParam(name="nDeptId", required= true) Integer nDeptId,
@NotNull @RequestParam(name="nRoomId" , required = true) Integer nRoomId,
@Nullable @RequestParam(name="nSubDeptId" , required = false) Integer nSubDeptId){
return ResponseEntity.ok(rdmService.unclaimedRoomAssign(nDeptId, nRoomId,nSubDeptId ));
}
TestRoomDepartmentMappingController class
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = RoomDepartmentMappingController.class)
@WebMvcTest(value = RoomDepartmentMappingController.class)
public class TestRoomDepartmentMappingController {
@Autowired
private MockMvc mockMvc;
@MockBean
RoomDepartmentMappingService roomDepartmentMappingService;
@Value("${InstituteIdentifier}")
String instituteIdentifier;
@Test
public void UnclaimedRoomAssignTest() throws Exception {
String stringUrl="/spacestudy/"+ instituteIdentifier+"/asset/room/assignUnclaimedRooms";
String stringResult = "Room assign sucessfully";
Department dep = new Department();
dep.setnDeptId(1);
RoomDepartmentMapping rdmObj = new RoomDepartmentMapping();
rdmObj.setnRoomAllocationId(587050);
rdmObj.setnDeptId(1);
rdmObj.setnRoomId(1215783);
String inputInJson = mapToJson(rdmObj);
Mockito.when(roomDepartmentMappingService.unclaimedRoomAssign(1, 1215783, 0)).thenReturn(stringResult);
mockMvc.perform(post(stringUrl)
.accept(MediaType.APPLICATION_JSON)
.content(inputInJson)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
Mockito.verify(roomDepartmentMappingService).unclaimedRoomAssign(1, 1215783, 0);
}
private String mapToJson(Object object) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(object);
}
}
Console
MockHttpServletRequest:
HTTP Method = POST
Request URI = /spacestudy/vcufy2010/asset/room/assignUnclaimedRooms
Parameters = {}
Headers = {Content-Type=[application/json], Accept=[application/json]}
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type = com.spacestudy.controller.RoomDepartmentMappingController
Method = public org.springframework.http.ResponseEntity<java.lang.String> com.spacestudy.controller.RoomDepartmentMappingController.unclaimedRoomAssign(java.lang.Integer,java.lang.Integer,java.lang.Integer)
Resolved Exception:
Type = org.springframework.web.bind.MissingServletRequestParameterException
MockHttpServletResponse:
Status = 400
Error message = Required Integer parameter 'nDeptId' is not present
Upvotes: 0
Views: 10643
Reputation: 36143
You have RequestParams that are required:
@NotNull @RequestParam(name="nDeptId", required= true) Integer nDeptId,
@NotNull @RequestParam(name="nRoomId" , required = true) Integer nRoomId,
But they re not present in your request URL
/spacestudy/vcufy2010/asset/room/assignUnclaimedRooms
this should look like
/spacestudy/vcufy2010/asset/room/assignUnclaimedRooms?nDeptId=x&nRoomId=x
where x should be the correct id
Upvotes: 2