Created
September 4, 2011 02:19
-
-
Save coderunner/1192128 to your computer and use it in GitHub Desktop.
test with easymock
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 TestRequestHandler | |
| { | |
| 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 = createMock(HttpRequest.class); | |
| context = createMock(Context.class); | |
| monitoring = createMock(Monitoring.class); | |
| store = createMock(Store.class); | |
| headers = createMock(Map.class); | |
| } | |
| @Test | |
| public void shouldReturnedAnUpperCaseVersionOfStoredText() | |
| { | |
| //record mode | |
| //record behavior and expected calls | |
| reset(request, context, monitoring, store, headers); | |
| expect(context.getMonitoring()).andReturn(monitoring); | |
| monitoring.incrementRequestCounter(); | |
| expect(request.getPath()).andReturn(REQUEST_PATH); | |
| expect(request.getHeaders()).andReturn(headers); | |
| expect(headers.get(CASE_HEADER)).andReturn(UPPERCASE); | |
| expect(context.getStore()).andReturn(store); | |
| expect(store.get(REQUEST_PATH)).andReturn(STORE_BODY); | |
| //enter replay mode | |
| replay(request, context, monitoring, store, headers); | |
| //test call | |
| HttpResponse response = requestHandler.handle(request, context); | |
| //assertions | |
| assertEquals(STORE_BODY.toUpperCase(), response.getBody()); | |
| assertEquals(200, response.getCode()); | |
| //verify mocks | |
| verify(request, context, monitoring, store, headers); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment