Junit测试用例失败400错误请求

时间:2018-09-28 15:16:55

标签: spring-data-jpa mockito junit4

我正在junit应用程序中使用spring data jpa进行单元测试。我正在尝试为控制器类unclaimedRoomAssign()编写junit测试用例。我正在使用mockito创建模拟对象。

我试图像下面这样编写测试,但是我收到测试失败400错误的请求。谁能告诉我测试用例中缺少的内容

RoomDepartmentMappingController类

   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类

@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

1 个答案:

答案 0 :(得分:1)

您有一些必需的RequestParams:

@NotNull  @RequestParam(name="nDeptId", required= true) Integer nDeptId,
@NotNull  @RequestParam(name="nRoomId" , required = true) Integer nRoomId,

但是它们不在您的请求网址中

/spacestudy/vcufy2010/asset/room/assignUnclaimedRooms

应该看起来像

/spacestudy/vcufy2010/asset/room/assignUnclaimedRooms?nDeptId=x&nRoomId=x

其中x应该是正确的ID