[英]ResponseBody is printing empty body for spring boot test
我正在按照這個來運行我的 spring-boot 控制器測試。 但是,我的代碼仍在打印一個空的正文。 我嘗試調用真正的方法並模擬了一個,這是 IntelliJ 的問題還是我錯過了什么?
下面是測試代碼。
@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());
}
}
下面是我的實際控制人
@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);
}
}
和我的服務等級:
@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);
}
}
這就是我的輸出的樣子
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 我們需要那個
確保您的映射器正常工作:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.