Created
November 2, 2015 21:25
-
-
Save Pacane/fb69ae49d72fc36dc219 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NotFoundResponse implements Response { | |
@Override | |
public int getStatusCode() { | |
return 404; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OkResponse implements Response { | |
@Override | |
public int getStatusCode() { | |
return 200; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Response { | |
int getStatusCode(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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