Skip to content

Instantly share code, notes, and snippets.

@Pacane
Created November 2, 2015 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pacane/fb69ae49d72fc36dc219 to your computer and use it in GitHub Desktop.
Save Pacane/fb69ae49d72fc36dc219 to your computer and use it in GitHub Desktop.
@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