gpt4 book ai didi

java - 我如何通过junits中的参数传递一个类?

转载 作者:行者123 更新时间:2023-12-01 19:21:12 25 4
gpt4 key购买 nike

我有一个 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));
}

我需要在参数中传递类arerequest 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);
}

关于java - 我如何通过junits中的参数传递一个类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59352253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com