Skip to content

Instantly share code, notes, and snippets.

@coderunner
Created September 5, 2011 00:46
Show Gist options
  • Select an option

  • Save coderunner/1193799 to your computer and use it in GitHub Desktop.

Select an option

Save coderunner/1193799 to your computer and use it in GitHub Desktop.

Revisions

  1. coderunner revised this gist Sep 5, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@ public class TestExampleRequestHandler_Mockito
    {
    private static final String STORE_BODY = "stored body";
    private static final String CASE_HEADER = "case";
    private static final String CASE_HEADER_VALUE = "uppercase";
    private static final String UPPERCASE = "uppercase";
    private static final String REQUEST_PATH = "path";

    private ExampleRequestHandler requestHandler;
    @@ -53,7 +53,7 @@ public void shouldIncrementRequestCounter()
    @Test
    public void shouldConvertToUpperCaseIfHeaderValuePresent()
    {
    when(headers.get(CASE_HEADER)).thenReturn(CASE_HEADER_VALUE);
    when(headers.get(CASE_HEADER)).thenReturn(UPPERCASE);

    HttpResponse response = requestHandler.handle(request, context);
    assertEquals(STORE_BODY.toUpperCase(), response.getBody());
  2. coderunner revised this gist Sep 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ public void shouldIncrementRequestCounter()
    }

    @Test
    public void shouldConvertToUpperCaseIfHeaderPresent()
    public void shouldConvertToUpperCaseIfHeaderValuePresent()
    {
    when(headers.get(CASE_HEADER)).thenReturn(CASE_HEADER_VALUE);

  3. coderunner created this gist Sep 5, 2011.
    61 changes: 61 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    public class TestExampleRequestHandler_Mockito
    {
    private static final String STORE_BODY = "stored body";
    private static final String CASE_HEADER = "case";
    private static final String CASE_HEADER_VALUE = "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 shouldConvertToUpperCaseIfHeaderPresent()
    {
    when(headers.get(CASE_HEADER)).thenReturn(CASE_HEADER_VALUE);

    HttpResponse response = requestHandler.handle(request, context);
    assertEquals(STORE_BODY.toUpperCase(), response.getBody());
    }
    }