Friday, August 27, 2010

Unit Testing Jersey REST service

In my last post i show how to create Rest service with Jersey api. I also try to give some idea about the basic concept of REST in this link. Now we should start testing before start to do anything else. I write one test case using Jersey Test Framework also create one client using Jersey client tool.

The Jersey Test Framework currently allows you to run your tests on any of the following three light weight containers:
• Embedded GlassFish
• Grizzly Web Server
• Lightweight HTTP Server
The framework is built over JUnit 4.x using Maven.
For details information on Jersey Test Framework check this post


Test case for ProductResource
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
import static org.junit.Assert.*;
import com.sun.jersey.test.framework.WebAppDescriptor;
import com.sun.jersey.test.framework.spi.container.http.HTTPContainerFactory;
import java.math.BigDecimal;
import org.model.Product;
import javax.ws.rs.core.MediaType;

/** *
* @author Kamal Hossain
*/
public class ProductServiceTest extends JerseyTest {

public static final String PACKAGE_NAME = "org.resources";
private WebResource ws;

public ProductServiceTest() {
super(new WebAppDescriptor.Builder(PACKAGE_NAME).build());
}

@Override
protected TestContainerFactory getTestContainerFactory() {
return new HTTPContainerFactory();

}

@Test
public void testProductResponse() throws UnsupportedEncodingException {
ws = resource().path("product/kamal");
ClientResponse response = ws.accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
assertEquals(200, response.getStatus());
System.out.println(response.getStatus());


}

@Test
public void testProductPrice() throws UnsupportedEncodingException {
ws = resource().path("product/kamal");
Product prod = ws.accept(MediaType.APPLICATION_XML).get(Product.class);
assertEquals(BigDecimal.valueOf(120), prod.getPrice());


}
}

Test case for ProductsResource
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.spi.container.TestContainerFactory;
import java.io.UnsupportedEncodingException;
import org.junit.Test;
import static org.junit.Assert.*;
import com.sun.jersey.test.framework.WebAppDescriptor;
import com.sun.jersey.test.framework.spi.container.http.HTTPContainerFactory;
import org.model.Product;
import java.util.Collection;
import java.util.List;
import javax.ws.rs.core.MediaType;

/** *
* @author kamal hossain
*/
public class ProductsServiceTest extends JerseyTest {
public static final String PACKAGE_NAME = 'org.resources';
private WebResource ws;

public ProductsServiceTest() {
super(new WebAppDescriptor.Builder(PACKAGE_NAME).build());
}

@Override
protected TestContainerFactory getTestContainerFactory() {
return new HTTPContainerFactory();

}

@Test
public void testProductResponse() throws UnsupportedEncodingException {
ws = resource().path("product/kamal");
ClientResponse response = ws.accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
assertEquals(200, response.getStatus());
System.out.println(response.getStatus());


}

@Test
public void testProductList() throws UnsupportedEncodingException {
ws = resource().path("products");
GenericType<Collection<Product>> genericType = new GenericType<Collection<Product>>(){};
List<Product> wsResult = (List<Product>) ws.accept(MediaType.APPLICATION_XML).get(genericType);
assertEquals(2, wsResult.size());

}    

}




You can also install firefox addon Poster to test services.

4 comments:

  1. Hi.. Thanks for the very nice and straight forward example. Can you please provide some examples how to get the response when we do "Put/Post". It will be very helpful.

    Thanks,
    Manju

    ReplyDelete
  2. ClientResponse response = ws.accept(MediaType.APPLICATION_JSON).get(ClientResponse.class);

    I am getting the folllowing error

    The method accept(MediaType...) in the type WebResource is not applicable for the arguments (MediaType)

    Please advise.

    ReplyDelete
  3. super(new WebAppDescriptor.Builder(PACKAGE_NAME).build());


    here. Error : The constructor JerseyTest(WebAppDescriptor) is undefined
    2 quick fixes available
    -Remove argument to match 'JerseyTest()'
    Cast argument 1 to

    ReplyDelete
  4. java.lang.Error: Unresolved compilation problems:
    The import com.sun.jersey.test.framework.spi.container.http.HTTPContainerFactory cannot be resolved
    HTTPContainerFactory cannot be resolved to a type

    at com.cloudlynq.rest.connect.ListFilesTest.(ListFilesTest.java:21)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:171)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:216)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:213)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
    at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

    ReplyDelete

AWS Services

      1.         Identity Access Management (IAM): Used to control Identity (who) Access (what AWS resources).                   1....