Я использую junit
для модульного тестирования в приложении spring data jpa
.Я пытаюсь написать тестовые случаи junit для класса контроллера unclaimedRoomAssign ().Я использую mockito
для создания фиктивного объекта.
Я пытался написать тест, как показано ниже, но я получаю тест неудачный 400 неверный запрос.Может ли кто-нибудь сказать мне, что мне не хватает в тестовых случаях
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);
}
}
Консоль
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