簡體   English   中英

我如何像junits中的params一樣傳遞一個類?

[英]How do i pass a class as in the params in junits?

我有一個 API:

@RequestMapping(value = "/area/create",
                method = RequestMethod.POST,
                produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<AreaEntry> createNewAreaEntry(
            @RequestBody AreaRequestDto areaRequestDto) throws ServiceException {
    UserContextHolder.setCustomer(areaRequestDto.getAreaEntry().getHost());
    UserContextHolder.setSsoId(areaRequestDto.getUser());
    AreaEntry newAreaEntryDto = service.createNewAreaEntry(areaRequestDto.getAreaEntry());
    System.out.println("The area entries" + areaRequestDto.getUser()
                       + "  " + " " 
                       + areaRequestDto.getAreaEntry().getName() 
                       + "  " 
                       + areaRequestDto.getAreaEntry().getCode() + " deugging");
    System.out.println("the new area entry dto is" + newAreaEntryDto);
    return new ResponseEntity<AreaEntry>(newAreaEntryDto, HttpStatus.OK);

我需要編寫一個 JUnit:

private List<String> getInvalidAreas(String areas) throws Exception {
    ResultActions actions = mockMvc.perform(get("/area/create").param("cities", areas));
}

我需要在 params 中傳遞類 arearequest dto 我該怎么做?

您可以簡單地傳遞 json 字符串,它會自動反序列化並轉換為 DTO。 或者您可以創建實用方法,將您的 dto 對象轉換為 json 並將其作為請求正文傳遞。

 public String asJsonString(final Object obj) throws JsonProcessingException {
        final ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        return mapper.writeValueAsString(obj);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM