我正在进行控制器集成测试。具有get映射的控制器返回Flux。
@Slf4j
@RequiredArgsConstructor
@RequestMapping("journal")
public class EntryController {
private final SomeService someService;
@GetMapping
public Flux<MyEntity> getEntity(
@RequestParam(required = false) String documentId,
@RequestParam(required = false) String docNumber){
return someService.getEntities( GetEntriesRequest.builder()
.documentId(documentId)
.docNumber(docNumber).build;
}
@PostMapping
public MyEntity postNew(@RequestBody @Valid MyEntity entity) {
return someService.save(entity);
}
}
.
最初,我试图像这样测试它。 (asJson方法以Json格式重新创建通过createdEntity方法创建的实体)。我已经测试了带有后映射的控制器,它正常工作。
@Test
void getEntriesByDocumentNumber() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(post("/journal")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(asJson(creatingEntity())))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andReturn();
String response = mvcResult.getResponse().getContentAsString();
String documentIdFromResponse =
JsonPath.parse(response).read("$.documentId");
this.mockMvc.perform(get("/journal")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.param("documentId", documentIdFromResponse))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk());
我不知道如何处理收到的回复。事实是响应主体为空,但是该方法有效,这可以在异步结果中看到。我没有找到如何获得asinc结果。而且我不能使用JsonPath,因为主体为空。
HTTP Method = GET
Request URI = /journal
Parameters = {documentId=[1590067372983-9ce4f563-520d-42ff-bd31-5b78befaf4b1]}
Headers = [Content-Type:"application/json;charset=UTF-8", Accept:"application/json"]
Body = null
Session Attrs = {}
Async:
Async started = true
Async result = [MyEntity(documentId=1590067372983-9ce4f563-520d-42ff-bd31-5b78befaf4b1, documentNumber=100)]
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 = []
我尝试使用WebTestClient,但是此代码不起作用。 没有名为“ webHandler”的bean可用
@SpringBootTest
@AutoConfigureMockMvc
class ApplicationTests {
@Autowired
private MockMvc mockMvc;
WebTestClient webTestClient;
@BeforeEach
void setUp(ApplicationContext context) {
webTestClient = WebTestClient.bindToApplicationContext(context).build();
}
@Test
void getEntityTest() throws Exception {
MvcResult mvcResult = this.mockMvc.perform(post("/journal")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(asJson(creatingEntity())))
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andReturn();
String response = mvcResult.getResponse().getContentAsString();
String documentIdFromResponse = JsonPath.parse(response).read("$.documentId");
webTestClient.get()
.uri("journal/")
.accept(MediaType.APPLICATION_JSON)
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$[0].documentId").isEqualTo(documentIdFromResponse);
}
}
我已经找到了。但是我不知道怎么用
@Bean
public WebHandler webHandler() {
//implement your bean initialization here
}