How to mock using Mockito?

Solution 1:

If you want to test your API, I would suggest Wiremock instead of Mockito.

@Test
public void shouldGetEndpoint() {
  String path = "/endpoint";
  wiremock.stubFor(get(urlEqualTo(path))
          .willReturn(aResponse()
                  .withStatus(200))
  );
}

If you want to write a unit test for your class by mocking attachmentService, you will need to do something like:

List<AttachmentBO> providedList = new ArrayList<>();
MyService attachmentService = Mockito.mock(MyService.class);
when(attachmentService.getAttachmentListForAnswer(any())).thenReturn(providedList);

Check out this tutorial