Skip to content
Mihail Kuznetsov edited this page Mar 16, 2015 · 3 revisions

Testing RESTful services

Testing

EverRest framework provides tools that should help to create test for your services. Here is simple example:

import org.everrest.core.RequestHandler;
import org.everrest.core.tools.ByteArrayContainerResponseWriter;
import org.everrest.core.tools.DependencySupplierImpl;
import org.everrest.core.tools.ResourceLauncher;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;

import junit.framework.TestCase;

public class MyTest extends TestCase
{
   protected ProviderBinder providers;

   protected ResourceBinderImpl resources;

   protected ResourceLauncher launcher;

   public void setUp() throws Exception
   {
      super.setUp();
      resources = new ResourceBinderImpl();
      providers = new ApplicationProviderBinder();
      RequestHandler requestHandler =
         new RequestHandlerImpl(resources, providers, new DependencySupplierImpl(), new EverrestConfiguration());
      launcher = new ResourceLauncher(requestHandler);
   }
   
   public void testMyResource() throws Exception
   {
      resources.addResource(MyResource.class, null);
      ByteArrayContainerResponseWriter writer = new ByteArrayContainerResponseWriter();
      ContainerResponse response = launcher.service("GET", "/test/anybody", "", null, null, writer, null);
      assertEquals(200, response.getStatus());
      assertEquals("hello anybody", new String(writer.getBody()));
   }
   
   @Path("test")
   public static class MyResource
   {
      @GET
      @Path("{name}")
      public String sayHello(@DefaultValue ("anonymous") @PathParam ("name") String name)
      {
         return "hello " + name;
      }
   }
}