Created
September 5, 2011 00:46
-
-
Save coderunner/1193799 to your computer and use it in GitHub Desktop.
test with mockito
This file contains hidden or 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 TestExampleRequestHandler_Mockito | |
| { | |
| private static final String STORE_BODY = "stored body"; | |
| private static final String CASE_HEADER = "case"; | |
| private static final String UPPERCASE = "uppercase"; | |
| private static final String REQUEST_PATH = "path"; | |
| private ExampleRequestHandler requestHandler; | |
| private HttpRequest request; | |
| private Context context; | |
| private Monitoring monitoring; | |
| private Store store; | |
| private Map<String, String> headers; | |
| @Before | |
| public void setup() | |
| { | |
| requestHandler = new ExampleRequestHandler(); | |
| request = mock(HttpRequest.class); | |
| context = mock(Context.class); | |
| monitoring = mock(Monitoring.class); | |
| store = mock(Store.class); | |
| headers = mock(Map.class); | |
| when(context.getMonitoring()).thenReturn(monitoring); | |
| when(context.getStore()).thenReturn(store); | |
| when(store.get(REQUEST_PATH)).thenReturn(STORE_BODY); | |
| when(request.getPath()).thenReturn(REQUEST_PATH); | |
| when(request.getHeaders()).thenReturn(headers); | |
| } | |
| @Test | |
| public void shouldReturnTheStoredBody() | |
| { | |
| HttpResponse response = requestHandler.handle(request, context); | |
| assertEquals(STORE_BODY, response.getBody()); | |
| } | |
| @Test | |
| public void responseCodeShouldBe200() | |
| { | |
| HttpResponse response = requestHandler.handle(request, context); | |
| assertEquals(200, response.getCode()); | |
| } | |
| @Test | |
| public void shouldIncrementRequestCounter() | |
| { | |
| requestHandler.handle(request, context); | |
| verify(monitoring).incrementRequestCounter(); | |
| } | |
| @Test | |
| public void shouldConvertToUpperCaseIfHeaderValuePresent() | |
| { | |
| when(headers.get(CASE_HEADER)).thenReturn(UPPERCASE); | |
| HttpResponse response = requestHandler.handle(request, context); | |
| assertEquals(STORE_BODY.toUpperCase(), response.getBody()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment