[英]ResponseBody is printing empty body for spring boot test
I am following this to run my spring-boot controller test.我正在按照这个来运行我的 spring-boot 控制器测试。 but however, my code is still printing an empty body.
但是,我的代码仍在打印一个空的正文。 I tried calling the real method and mocked one, is it something IntelliJ issue or am I missing something?
我尝试调用真正的方法并模拟了一个,这是 IntelliJ 的问题还是我错过了什么?
below is the test code.下面是测试代码。
@RunWith(SpringRunner.class)
@WebMvcTest(AnimalController.class)
class AnimalControllerTest {
@Autowired
MockMvc mvc;
@MockBean
AnimalService animalService;
@Before
public void setUp(){
AnimalDto response = new AnimalDto();
response.setId(1l);
when(animalService.add(any())).thenReturn(response);
}
@Test
void addAnimal() throws Exception {
AnimalDto animalDto = new AnimalDto();
mvc.perform(MockMvcRequestBuilders
.post("/animal/add")
.content(asJsonString(new AnimalDto(null, "dog", null,null, null, null, null)))
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.jsonPath("$.id").exists());
}
}
below is my actual controller下面是我的实际控制人
@RestController
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@RequestMapping("/animal")
public class AnimalController {
AnimalService animalService;
@PostMapping("/add")
public AnimalDto addAnimal(@Valid @RequestBody AnimalDto animalDto){
return animalService.add(animalDto);
}
}
and my service class:和我的服务等级:
@Service
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
@Slf4j
public class AnimalService {
AnimalRepository animalRepository;
SourceToDestinationMapper mapper;
public AnimalDto add(AnimalDto animalDto) {
Animal animal = mapper.animalDtoToAnimal(animalDto);
Animal savedAnimal = animalRepository.save(animal);
log.debug("animal object saved:",animal);
return mapper.animalToAnimalDto(savedAnimal);
}
}
this is how my output looks like这就是我的输出的样子
MockHttpServletRequest:
HTTP Method = POST
Request URI = /animal/add
Parameters = {}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json", Content-Length:"103"]
Body = {"id":null,"title":"dog","located":null,"type":null,"preference":null,"favoriteRooms":null,"room":null}
Session Attrs = {}
Handler:
Type = com.self.zoo.controller.AnimalController
Method = com.self.zoo.controller.AnimalController#addAnimal(AnimalDto)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = []
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: No value at JSON path "$.id"
at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:304)
at org.springframework.test.util.JsonPathExpectationsHelper.assertExistsAndReturn(JsonPathExpectationsHelper.java:328)
at org.springframework.test.util.JsonPathExpectationsHelper.exists(JsonPathExpectationsHelper.java:192)
at org.springframework.test.web.servlet.result.JsonPathResultMatchers.lambda$exists$3(JsonPathResultMatchers.java:123)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:212)
at com.self.zoo.controller.AnimalControllerTest.addAnimal(AnimalControllerTest.java:54)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
我最终写了@BeforeAll注释,因为在 Junit5 我们需要那个
Make sure your mapper works correctly:确保您的映射器正常工作:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.