This post will demonstrate how to step up the project and write a first test.
Start by building the project, from the command line run this:
mvn -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=webapp-javaee6 -DarchetypeVersion=1.5 -DarchetypeRepository=http://repo.maven.apache.org/maven2 -DgroupId=com.creativesci.tutorial -DartifactId=jersey -Dversion=1.0-SNAPSHOT -Dpackage=com.creativesci.tutorial.jersey -DinteractiveMode=false archetype:generate
Update the pom.xml dependecies section to look like this:
<dependencies>
<dependency>
<groupid>com.sun.jersey</groupid>
<artifactid>jersey-server</artifactid>
<version>1.12</version>
</dependency>
<dependency>
<groupid>com.sun.jersey</groupid>
<artifactid>jersey-json</artifactid>
<version>1.12</version>
</dependency>
<dependency>
<groupid>junit</groupid>
<artifactid>junit</artifactid>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupid>com.sun.jersey.jersey-test-framework</groupid>
<artifactid>jersey-test-framework-inmemory</artifactid>
<version>1.12</version>
<scope>test</scope>
</dependency>
</dependencies>
Now create a new file in src/test/com/creativesci/tutorial/jersey called PersonServiceTest.java. This class will extend JerseyTest and we have to add a constructor.
public PersonServiceTest() throws Exception {
super("com.creativesci.tutorial.jersey");
}
Now we can add our first test method. Since we are doing baby steps we want a test which will generate the fewest number of lines of production code. I think that the get all will be the easiest to implement first, at least it will allow us to test other actions later.
@Test
public void testGetAllPeopleReturnsEmptyList() {
WebResource webResource = resource(); // test REST client
List<Person> allPeople = webResource.path("person").accept(MediaType.APPLICATION_JSON).get(new GenericType<List<Person>>(){});
assertTrue(allPeople.isEmpty());
}
So there is the first test, of course this currently fails to execute, but that's TDD. Next post we'll run through creating the PersonService and Person classes so that this test can pass.