0

Controller

  @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));
        });
  }

Test Class

  @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());

  }
}

When i run the test it's actually calling original service it's not using mock service. Can someone help me how to write a test for above controller method.

When i print the mock service invocations it's printing below output.

  [Mockito] Unused stubbings of: eventService
  1. eventService.updateEvent("", null, "");
  - stubbed -> at com.endpoint.rest.controller.EventStreamControllerTest.testEventConformation(EventStreamControllerTest.java:158)
deadshot
  • 8,881
  • 4
  • 20
  • 39
  • Did you set a breakpoint in your tested controller if it does call the method and with which parameters? And on which object, is it the `eventService` mock you're preparing in your test? It would be best you show the whole relevant test class code around this method, including declarations and setup. – daniu Feb 13 '20 at 09:43
  • You should how more about the test part : how you declare/init the mock and how you configure the test runner – davidxxx Feb 13 '20 at 10:06
  • @daniu I am added the whole test class in my post – deadshot Feb 13 '20 at 10:30
  • It looks like the `serviceAuthClient` does not get mocked, which begs the question what `serviceAuthClient.getDefaultJwtTokenObserver().getJwtAccessToken()` returns. Is it `null`? – daniu Feb 13 '20 at 11:24
  • i also mocked the ServiceAuthClient – deadshot Feb 13 '20 at 11:32
  • 1
    Try mocking them with `@MockBean` instead of `@Mock`. Also you need to mock the `serviceAuthClient.getDefaultJwtTokenObserver()` call. Also you should try the breakpoint thing. – daniu Feb 13 '20 at 12:35
  • thanks it's working – deadshot Feb 14 '20 at 08:36

1 Answers1

-1

In you test class, you should define your eventService as a MockBean. Then it will be a mock service.

@MockBean
private EventService eventService;
Tran Ho
  • 1,442
  • 9
  • 15