Although this has been written by many and available on the net, I thought of sharing this anyways to make it easily available through google search for the people looking of this.
In one of my applications, we had written a servlet wrapper to existing Web Services to accept and convert the Web Service output as a JSON strings.
To test the functionality of the servlet proxy, of course, we had used commons httpclient library.
1: HttpClient httpclient = new HttpClient();
2: PostMethod httpPost = new PostMethod(baseUrl + "/ProxyClient");
3: System.out.println("Testing Authentication - Executing request " + httpPost.getURI());
4: 5: Map<String, String> map = new HashMap<String, String>();
6: 7: map.put(JSONConstants.operation, JSONConstants.OPERATION_AUTHENTICATE);8: map.put(JSONConstants.userId, "ravi");
9: map.put(JSONConstants.password, "ffss123");
10: map.put(JSONConstants.imeiNo, "12345566");
11: map.put(JSONConstants.pdaVersion, "3.1");
12: 13: JSONObject jsonObject = JSONObject.fromObject(map); 14: 15: NameValuePair[] data = { new NameValuePair("q", jsonObject.toString()) };
16: 17: httpPost.setRequestBody(data); 18: 19: httpclient.executeMethod(httpPost); 20: responseBody = httpPost.getResponseBodyAsString();21: System.out.println("RESPONSE : " + responseBody);
Listing: 1
We actually wanted to write a JUnit test case for testing the various JSON service calls made to this servlet proxy. After some searching we found that the Jetty servlet-container has a ServletTester class just for this purpose.
In our maven project we included the ServletTester using the following dependency information. If however, you don't use Maven, you will need to manually download all of the dependencies.
1: <dependency>
2: <groupId>org.mortbay.jetty</groupId>
3: <artifactId>jetty-servlet-tester</artifactId>
4: <version>6.1.6</version>
5: <scope>test</scope>
6: </dependency>
Listing: 2
With this in place the next step was writing the JUnit test cases. We wanted to initialise the servlet-container just once, then run a set of tests against it. In JUnit 4 you can use the @BeforeClass and @AfterClass annotations to mark methods that should be executed before and after all of the tests.
1: public class TestJSON
2: { 3: 4: private static ServletTester tester;
5: 6: private static String baseUrl;
7: 8: /**
9: * This kicks off an instance of the Jetty
10: * servlet container so that we can hit it.
11: * We register an echo service that simply
12: * returns the parameters passed to it.
13: */
14: @BeforeClass15: public static void initServletContainer () throws Exception
16: {17: tester = new ServletTester();
18: tester.setContextPath("/");
19: tester.addServlet(ProxyClient.class, "/ProxyClient");
20: baseUrl = tester.createSocketConnector(true); // This makes the jetty to bind the servlet container to a
21: // higher port value dynamically. Hence we need to retain
22: // the baseUrl reference to be used in our test methods.
23: tester.start(); 24: } 25: 26: /**
27: * Stops the Jetty container.
28: */
29: @AfterClass30: public static void cleanupServletContainer () throws Exception
31: { 32: tester.stop(); 33: } 34: }Listing: 3
Now all we had to do is wrap the test code in Listing: 1 in a method and annotate it with @Test. And we were able to run the tests out-of-container along with other tests.
If you want to more comprehensively test your servlets and other JEE stuff then the following is a list of a few unit testing libraries that will help you a lot to make it easier.
1. Jakarta Cactus
2. Spring-Mock
3. HttpUnit
No comments:
Post a Comment