Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
@RunWith(JukitoRunner.class)
public class EndpointTest {
public static class Module extends JukitoModule {
@Override
protected void configureTest() {
install(new FactoryModuleBuilder().build(ResponseFactory.class));
}
}
private final String A_USERNAME = "Bobby";
private final int USER_ID = 1337;
@Inject
private UserInformationService userInformationService;
@Inject
private Endpoint endpoint;
@Test
public void getUsername_delegatesToUserInformationService() {
endpoint.getUsername(USER_ID);
verify(userInformationService).getUsername(USER_ID);
}
@Test
public void getUsername_returnsOkWhenUserIsFound() {
given(userInformationService.getUsername(USER_ID)).willReturn(A_USERNAME);
int okResponseCode = javax.ws.rs.core.Response.Status.OK.getStatusCode();
Response result = endpoint.getUsername(USER_ID);
assertThat(result.getStatusCode()).isEqualTo(okResponseCode);
}
@Test
public void getUser_returnsNotFoundWhenUserIsNotFound() {
given(userInformationService.getUsername(USER_ID)).willThrow(NotFoundException.class);
int notFoundStatusCode = javax.ws.rs.core.Response.Status.NOT_FOUND.getStatusCode();
Response result = endpoint.getUsername(USER_ID);
assertThat(result.getStatusCode()).isEqualTo(notFoundStatusCode);
}
}
public class NotFoundResponse implements Response {
@Override
public int getStatusCode() {
return 404;
}
}
public class OkResponse implements Response {
@Override
public int getStatusCode() {
return 200;
}
}
public interface Response {
int getStatusCode();
}
public interface ResponseFactory {
OkResponse createOk(String username);
NotFoundResponse createNotFound();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment