Skip to content

Instantly share code, notes, and snippets.

@biemond
Created July 31, 2014 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biemond/982dc058c419e160dc46 to your computer and use it in GitHub Desktop.
Save biemond/982dc058c419e160dc46 to your computer and use it in GitHub Desktop.
PersonResourceTest
package test.com.example.rest.services;
import com.example.rest.entities.Person;
import com.example.rest.services.PersonResource;
import java.util.List;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import org.junit.Test;
public class PersonResourceTest extends JerseyTest {
public PersonResourceTest() {
}
@Override
protected Application configure(){
return new ResourceConfig(PersonResource.class);
}
@Test
public void findAll() {
final Response result = target("person").request()
.accept(MediaType.APPLICATION_JSON)
.get(Response.class);
if(result.getStatus() != Response.Status.OK.getStatusCode()){
fail("Wrong status code "+result.getStatus());
}
List<Person> persons = result.readEntity(new GenericType<List<Person>>(){});
assertEquals(2,persons.size());
for ( Person person : persons) {
assertNotNull(person.getId());
if ( person.getId() == 1 ) {
assertEquals("Edwin",person.getFirstName());
}
if ( person.getId() == 2 ) {
assertEquals("Mark",person.getFirstName());
}
}
}
@Test
public void findPerson() {
final Response result = target("person/1").request()
.accept(MediaType.APPLICATION_JSON)
.get(Response.class);
if(result.getStatus() != Response.Status.OK.getStatusCode()){
fail("Wrong status code "+result.getStatus());
}
Person person = result.readEntity(new GenericType<Person>(){});
assertEquals("Edwin",person.getFirstName());
}
@Test
public void findPersonNotfound() {
final Response result = target("person/100").request()
.accept(MediaType.APPLICATION_JSON)
.get(Response.class);
assertEquals(result.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment