Resteasy, Make a Http-Put or Http-Post with Server-side Mock Framework of RestEasy

http, jax-rs, rest, resteasy, unit-testing

Solution

A bit late response but , might have some use for someone. This is how i usually test my PUT requests. Hope it helps

@Test
public void testPUT() throws Exception {      
      POJOResourceFactory noDefaults = new POJOResourceFactory(REST.class);

      Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
      dispatcher.getRegistry().addResourceFactory(noDefaults);

      String url = "your_url_here";   

      MockHttpRequest request = MockHttpRequest.put(url);
      request.accept(MediaType.APPLICATION_JSON);
      request.contentType(MediaType.APPLICATION_JSON_TYPE);
      // res being your resource object
      request.content(res.toJSONString().getBytes());

      MockHttpResponse response = new MockHttpResponse();
      dispatcher.invoke(request, response);

    Assert.assertEquals( HttpStatus.SC_CREATED,response.getStatus());

}

Problem

I wrote a Rest-Service which i would like to test. I wanna run a JUnit test without having my server run. For this I am using the Server-side Mock Framework of RestEasy. My question is, how can I make a Http-Put or Http-Post request with this framework with an marshalled Java Object in the Http-Body??? The Code below runs fine for an Http-Get, but how to make a Put or Post, maybe someone got some example code for this??? ``` @Test public void testClient() throws Exception { Dispatcher dispatcher = MockDispatcherFactory.createDispatcher(); POJOResourceFactory noDefaults = new POJOResourceFactory( MyClass.class); dispatcher.getRegistry().addResourceFactory(noDefaults); { MockHttpRequest request = MockHttpRequest.get("/message/test/" + requestParam); MockHttpResponse response = new MockHttpResponse(); dispatcher.invoke(request, response); assertEquals(HttpServletResponse.SC_OK, response.getStatus()); } } ```

Original source