BAEL-2268 - Guide to JerseyTest (#5443)
* BAEL-2268 - Guide to JerseyTest - second attempt without formatting changes * BAEL-2268 - Guide to JerseyTest - Add line break to end of file
This commit is contained in:
parent
f61d503d1f
commit
ca4b6200a7
|
@ -54,4 +54,9 @@ public class Fruit {
|
|||
public void setSerial(String serial) {
|
||||
this.serial = serial;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Fruit [name: " + getName() + " colour: " + getColour() + "]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,8 @@ import javax.ws.rs.Path;
|
|||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import org.glassfish.jersey.server.mvc.ErrorTemplate;
|
||||
import org.glassfish.jersey.server.mvc.Template;
|
||||
|
@ -86,6 +88,16 @@ public class FruitResource {
|
|||
public void createFruit(@Valid Fruit fruit) {
|
||||
SimpleStorageService.storeFruit(fruit);
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/created")
|
||||
@Consumes(MediaType.APPLICATION_JSON)
|
||||
public Response createNewFruit(@Valid Fruit fruit) {
|
||||
String result = "Fruit saved : " + fruit;
|
||||
return Response.status(Status.CREATED.getStatusCode())
|
||||
.entity(result)
|
||||
.build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Valid
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.jersey.server;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.ws.rs.core.Application;
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
import org.glassfish.jersey.test.JerseyTest;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GreetingsResourceIntegrationTest extends JerseyTest {
|
||||
|
||||
@Override
|
||||
protected Application configure() {
|
||||
return new ResourceConfig(Greetings.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGetHiGreeting_whenCorrectRequest_thenResponseIsOkAndContainsHi() {
|
||||
Response response = target("/greetings/hi").request()
|
||||
.get();
|
||||
|
||||
assertEquals("Http Response should be 200: ", Status.OK.getStatusCode(), response.getStatus());
|
||||
assertEquals("Http Content-Type should be: ", MediaType.TEXT_HTML, response.getHeaderString(HttpHeaders.CONTENT_TYPE));
|
||||
|
||||
String content = response.readEntity(String.class);
|
||||
assertEquals("Content of ressponse is: ", "hi", content);
|
||||
}
|
||||
}
|
|
@ -10,6 +10,7 @@ import javax.ws.rs.core.Application;
|
|||
import javax.ws.rs.core.Form;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.Response.Status;
|
||||
|
||||
import org.glassfish.jersey.test.JerseyTest;
|
||||
import org.glassfish.jersey.test.TestProperties;
|
||||
|
@ -63,6 +64,15 @@ public class FruitResourceIntegrationTest extends JerseyTest {
|
|||
assertEquals("Http Response should be 400 ", 400, response.getStatus());
|
||||
assertThat(response.readEntity(String.class), containsString("Fruit colour must not be null"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCreateFruit_whenJsonIsCorrect_thenResponseCodeIsCreated() {
|
||||
Response response = target("fruit/created").request()
|
||||
.post(Entity.json("{\"name\":\"strawberry\",\"weight\":20}"));
|
||||
|
||||
assertEquals("Http Response should be 201 ", Status.CREATED.getStatusCode(), response.getStatus());
|
||||
assertThat(response.readEntity(String.class), containsString("Fruit saved : Fruit [name: strawberry colour: null]"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUpdateFruit_whenFormContainsBadSerialParam_thenResponseCodeIsBadRequest() {
|
||||
|
@ -102,6 +112,23 @@ public class FruitResourceIntegrationTest extends JerseyTest {
|
|||
.get(String.class);
|
||||
assertThat(json, containsString("{\"name\":\"strawberry\",\"weight\":20}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFruitExists_whenSearching_thenResponseContainsFruitEntity() {
|
||||
Fruit fruit = new Fruit();
|
||||
fruit.setName("strawberry");
|
||||
fruit.setWeight(20);
|
||||
Response response = target("fruit/create").request(MediaType.APPLICATION_JSON_TYPE)
|
||||
.post(Entity.entity(fruit, MediaType.APPLICATION_JSON_TYPE));
|
||||
|
||||
assertEquals("Http Response should be 204 ", 204, response.getStatus());
|
||||
|
||||
final Fruit entity = target("fruit/search/strawberry").request()
|
||||
.get(Fruit.class);
|
||||
|
||||
assertEquals("Fruit name: ", "strawberry", entity.getName());
|
||||
assertEquals("Fruit weight: ", Integer.valueOf(20), entity.getWeight());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFruit_whenFruitIsInvalid_thenReponseContainsCustomExceptions() {
|
||||
|
|
Loading…
Reference in New Issue