控制器
@PostMapping("/event/confirmation")
public Mono<CallbackResponse> eventConfirmation(@RequestParam("eventId") String eventId,
@RequestBody ExecutionSummary execSummary, @RequestHeader("customerCode") String customerCode)
throws ResourceNotFoundException {
return eventService
.updateEvent(eventId, execSummary, serviceAuthClient.getDefaultJwtTokenObserver().getJwtAccessToken())
.flatMap(event -> {
return Mono.just(new CallbackResponse(true));
});
}
测试等级
@RunWith(SpringRunner.class)
@WebAppConfiguration
@AutoConfigureMockMvc
@SpringBootTest(properties = "spring.main.banner-mode=off")
public class EventStreamControllerTest {
@Mock
private ServiceAuthClient serviceAuthClient;
@Mock
private EventService eventService;
@InjectMocks
private EventStreamController eventStreamController;
@Before
public void setUp() {
Mockito.reset(eventService);
mvc = MockMvcBuilders.standaloneSetup(eventStreamController)
.setControllerAdvice(new ControllerExceptionHandler()).build();
}
@Test
public void testEventConformation() throws Exception {
ExecutionSummary executionSummary = new ExecutionSummary();
executionSummary.setErrorMessage("Test");
Mono<Event> event = Mono.just(new Event());
Mockito.when(eventService.updateEvent(Mockito.anyString(),
Mockito.any(), Mockito.anyString()))
.thenReturn(event);
RequestBuilder requestBuilder =
MockMvcRequestBuilders.post("/event/confirmation")
.contentType(MediaType.APPLICATION_JSON).header("customerCode", "456")
.param("eventId", "123")
.content(objectMapper.writeValueAsString(executionSummary));
MvcResult result = mvc.perform(requestBuilder).andReturn();
assertEquals(HttpStatus.OK.value(),result.getResponse().getStatus());
}
}
当我运行测试时,它实际上是在调用原始服务,而不是使用模拟服务。有人能帮我写一个测试上述控制器的方法。
当我打印模拟服务调用时,它打印在输出下面。
[Mockito] Unused stubbings of: eventService
1. eventService.updateEvent("", null, "");
- stubbed -> at com.endpoint.rest.controller.EventStreamControllerTest.testEventConformation(EventStreamControllerTest.java:158)
1条答案
按热度按时间jk9hmnmh1#
在测试类中,应该定义
eventService
作为一个MockBean
. 那么这将是一个模拟服务。