@Test
public void performPut() throws Exception {
JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class);
Request requestMock = EasyMock.createNiceMock(Request.class);
Response responseMock = EasyMock.createNiceMock(Response.class);
expect(requestMock.getMethod()).andReturn(Method.PUT).anyTimes();
String[] contentTypeHeader = {"text/plain"};
expect(requestMock.getHeaderValues(HeaderName.CONTENT_TYPE)).andReturn(contentTypeHeader).anyTimes();
//this redundancy makes me prefer not to use mocks
Set<HeaderName> headerNames = new HashSet<HeaderName>();
headerNames.add(HeaderName.CONTENT_TYPE);
expect(requestMock.getHeaderNames()).andReturn(headerNames).anyTimes();
final String message = "The message in the body";
expect(requestMock.getMessageBody()).andReturn(new MessageBody2Write() {
@Override
public void writeTo(WritableByteChannel out) throws IOException {
out.write(ByteBuffer.wrap(message.getBytes()));
}
});
RequestURI requestURI = EasyMock.createNiceMock(RequestURI.class);
expect(requestURI.getPath()).andReturn("/");
expect(requestMock.getRequestURI()).andReturn(requestURI).anyTimes();
replay(requestMock);
replay(requestURI);
replay(responseMock);
handler.handle(requestMock, responseMock);
Assert.assertEquals(message, receivedBody);