Saturday, November 17, 2012

Basic TDD with Jersey Test Framework

Decided to do TDD on one of  my mobileTAO initiatives. I hope this may also help others who wish to learn TDD with Jersey test framework

1.Requirements

1. JDK 1.6
2. Eclipse Indigo
3. Jersey
4. Jersey Test Framework
5. Maven 3.0.4 (with m2eclipse plugin)

2. Create maven project

run
mvn archetype:generate -DgroupId=desired.group.id -DartifactId=desiredArtifactId -Darchetype.ArtifactId=maven-archetype-webapp -DinteractiveMode=false

run mvn eclipse:eclipse in order to support eclipse

 Import project to eclipse

3. Setting up Dependencies 

Add the jersey dependency to your pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.qoutepad.rest</groupId>
<artifactId>QoutePadJersey</artifactId>
<packaging>war</packaging>
<version>1</version>
<name>QoutePadJersey Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<jersey.version>1.15</jersey.version>
</properties>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-inmemory</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>snapshot-repository.java.net</id>
<name>Java.net Snapshot Repository for Maven</name>
<url>https://maven.java.net/content/repositories/snapshots/</url>
<layout>default</layout>
</repository>
</repositories>
<build>
<finalName>QoutePadJersey</finalName>
</build>
</project>
view raw pom.xml hosted with ❤ by GitHub

4. Create the test 

We will now code our unit test. add the source code to src/test/java

package org.qoutepad.test;
import static org.junit.Assert.*;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.test.framework.JerseyTest;
import org.junit.Test;
import org.qoutepad.rest.HelloJSTService;
public class HelloJST extends JerseyTest {
public HelloJST(){
super(HelloJSTService.class.getPackage().getName());
}
@Test
public void helloJSTTest(){
WebResource resource = resource();
String message = resource.path("helloJST").get(String.class);
assertEquals("HelloJST", message);
}
}
view raw HelloJST.java hosted with ❤ by GitHub

5. Create rest service


package org.qoutepad.rest;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
@Path("/helloJST")
public class HelloJSTService {
@GET
public String getMessage(){
return new String("HelloJST");
}
}


Usefull Links
http://jersey.java.net/nonav/documentation/latest/getting-started.html
http://www.parleys.com/#st=5&id=2723&sl=0

3 comments:

Unknown said...

cool!!!

Bert Añasco said...

LOL

OMKAR said...

Can you give more info on how we do TDD with some ATDD framework like Cucumber