1.Requirements
1. JDK 1.62. Eclipse Indigo
3. Jersey
4. Jersey Test Framework
5. Maven 3.0.4 (with m2eclipse plugin)
2. Create maven project
runmvn 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
4. Create the test
We will now code our unit test. add the source code to src/test/java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
5. Create rest service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
http://jersey.java.net/nonav/documentation/latest/getting-started.html
http://www.parleys.com/#st=5&id=2723&sl=0
3 comments:
cool!!!
LOL
Can you give more info on how we do TDD with some ATDD framework like Cucumber
Post a Comment