Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
@RunWith(MockitoJUnitRunner.class)
public class EndpointTestWithMockito {
private final String A_USERNAME = "Bobby";
private final int USER_ID = 1337;
@Mock
private UserInformationService userInformationService;
@Mock
private ResponseFactory responseFactory;
private Endpoint endpoint;
@Before
public void setUp() throws Exception {
endpoint = new Endpoint(userInformationService, responseFactory);
}
@Test
public void getUsername_delegatesToUserInformationService() {
endpoint.getUsername(USER_ID);
verify(userInformationService).getUsername(USER_ID);
}
@Test
public void getUsername_returnsOkWhenUserIsFound() {
Response OK_RESPONSE = new OkResponse();
given(responseFactory.createOk(A_USERNAME)).willReturn(OK_RESPONSE);
given(userInformationService.getUsername(USER_ID)).willReturn(A_USERNAME);
Response result = endpoint.getUsername(USER_ID);
assertThat(result).isSameAs(OK_RESPONSE);
}
@Test
public void getUser_returnsNotFoundWhenUserIsNotFound() {
Response NOT_FOUND_RESPONSE = new NotFoundResponse();
given(responseFactory.createNotFound()).willReturn(NOT_FOUND_RESPONSE);
given(userInformationService.getUsername(USER_ID)).willThrow(NotFoundException.class);
Response result = endpoint.getUsername(USER_ID);
assertThat(result).isSameAs(NOT_FOUND_RESPONSE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment