Close

JAX-RS - JerseyTest Examples

JAX-RS JAVA EE 

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.BadRequestException;
import javax.ws.rs.core.Application;

public class OrderServiceTest extends JerseyTest {

@Override
protected Application configure() {
return new ResourceConfig(OrderService.class);
}

@Test
public void ordersPathParamTest() {
String response = target("orders/4534/items/e34").request().get(String.class);
Assert.assertTrue("order id: 4534 and item id: e34".equals(response));

response = target("orders/4534567/items/u343").request().get(String.class);
Assert.assertTrue("order id: 4534567 and item id: u343".equals(response));
}

@Test(expected = BadRequestException.class)
public void ordersPathParamBadRequestTest() {
String response = target("orders/tt343/items/554").request().get(String.class);
Assert.assertTrue("The requested order id or item id are not valid. order id: tt343 and item id: 554".
equals(response));

}
}
Original Post




import com.logicbig.example.api.ItemRestService;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import javax.ws.rs.HeaderParam;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
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 java.math.BigDecimal;
import java.util.List;

public class ItemRestServiceTest extends JerseyTest {
@Override
protected Application configure() {
return new ResourceConfig(ItemRestService.class);
}

@Test
public void customerRestServiceFormTest() throws Exception {

Item item = new Item();
item.setName("Internal HDD");
item.setPrice(new BigDecimal(50));

WebTarget target = target("items");
//put new resource at /items/10
Response response = target.path("10").request()
.put(Entity.entity(item, MediaType.APPLICATION_JSON));
System.out.println(response.getHeaderString("Location"));
System.out.println(response.getStatus());

//put one more item at /items/20
item = new Item();
item.setName("Varion Laptop");
item.setPrice(new BigDecimal(450));
response = target.path("20").request(MediaType.APPLICATION_JSON)
.put(Entity.entity(item, MediaType.APPLICATION_JSON));
System.out.println(response.getHeaderString("Location"));
System.out.println(response.getStatus());

// try to put the same resource will generate 204 status code
response = target.path("20").request(MediaType.APPLICATION_JSON)
.put(Entity.entity(item, MediaType.APPLICATION_JSON));
System.out.println(response.getHeaderString("Location"));
System.out.println(response.getStatus());


//get all items at resource /items
List<Item> items = target.request(MediaType.APPLICATION_JSON)
.get(new GenericType<List<Item>>() {
});
//note in above line we used GenericType
// cause readEntity(class) cannot handle the generics
System.out.println("all items: "+items);


}
}
Original Post




import com.logicbig.example.api.CustomerRestService;
import com.logicbig.example.client.ClientUtil;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;

public class CustomerRestServiceTest extends JerseyTest {

@Override
protected Application configure() {
return new ResourceConfig(CustomerRestService.class);
}

@Test
public void customerRestServiceRawTest() {
String joe = ClientUtil.createCustomerInJSON("Joe", "435 West Dr, Peejay", "555-456-9877");
String response = target("customers").request()
.post(Entity.entity(joe, MediaType.APPLICATION_JSON),
String.class);
System.out.println(response);
}

@Test
public void customerRestServiceTest() {
Customer newCustomer = new Customer();
newCustomer.setName("Jake Mae");
newCustomer.setAddress("342, Snake Dr, GreenLake");
newCustomer.setPhoneNumber("444-333-4564");
String response = target("customers")
.request(MediaType.APPLICATION_JSON)
.accept(MediaType.TEXT_PLAIN_TYPE)
//this time we are calling post to make a HTTP POST call
.post(Entity.json(newCustomer), String.class);


System.out.println(response);
}
}
Original Post




import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.core.Application;

public class OrderServiceTest extends JerseyTest {

@Override
protected Application configure() {
return new ResourceConfig(OrderService.class);
}

@Test
public void ordersPathParamTest() {
String response = target("orders/4534").request().get(String.class);
Assert.assertTrue("orderId: 4534".equals(response));

response = target("orders/453").request().get(String.class);
Assert.assertTrue("orderId: 453".equals(response));

response = target("orders/a453").request().get(String.class);
Assert.assertTrue("orderId: a453".equals(response));

response = target("orders/s45").request().get(String.class);
Assert.assertTrue("orderId: s45".equals(response));

}
}
Original Post
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.client.Invocation;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;

public class OrderServiceTest extends JerseyTest {

@Override
protected Application configure() {
return new ResourceConfig(OrderService.class);
}

@Test
public void ordersPathParamTest() {
Invocation.Builder request = target("orders/4534").request();
String response = request.accept(MediaType.APPLICATION_JSON_TYPE)
.get(String.class);
System.out.println(response);
Assert.assertTrue("{\"orderId\": \"4534\", \"customer\": John}".equals(response));


request = target("orders/4534").request();
response = request.accept(MediaType.TEXT_PLAIN_TYPE).get(String.class);
System.out.println(response);
Assert.assertTrue("returning order with id 4534".equals(response));
}
}
Original Post




import org.glassfish.jersey.server.ResourceConfig; 
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Assert;
import org.junit.Test;

import javax.ws.rs.core.Application;

public class OrderServiceTest extends JerseyTest {

@Override
protected Application configure() {
return new ResourceConfig(OrderService.class);
}

@Test
public void ordersPathParamTest() {
String response = target("orders/4534/items/e34").request().get(String.class);
Assert.assertTrue("order id: 4534 and item id: e34".equals(response));

response = target("orders/4534567/items/u343").request().get(String.class);
Assert.assertTrue("order id: 4534567 and item id: u343".equals(response));

response = target("orders/tt343/items/554").request().get(String.class);
Assert.assertTrue("The requested order id or item id are not valid. order id: tt343 and item id: 554".
equals(response));
}
}
Original Post




import com.logicbig.example.api.ItemRestService;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
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 java.math.BigDecimal;
import java.util.List;

public class ItemRestServiceTest extends JerseyTest {
@Override
protected Application configure() {
return new ResourceConfig(ItemRestService.class);
}

@Test
public void customerRestServiceFormTest() throws Exception {

Item item = new Item();
item.setName("Internal HDD");
item.setPrice(new BigDecimal(50));

WebTarget target = target("items");
//put new resource at /items/10
Response response = target.path("10").request()
.put(Entity.entity(item, MediaType.APPLICATION_JSON));
System.out.println(response.getHeaderString("Location"));
System.out.println(response.getStatus());

//put one more item at /items/20
item = new Item();
item.setName("Varion Laptop");
item.setPrice(new BigDecimal(450));
response = target.path("20").request(MediaType.APPLICATION_JSON)
.put(Entity.entity(item, MediaType.APPLICATION_JSON));
System.out.println(response.getHeaderString("Location"));
System.out.println(response.getStatus());

// try to put the same resource will generate 204 status code
response = target.path("20").request(MediaType.APPLICATION_JSON)
.put(Entity.entity(item, MediaType.APPLICATION_JSON));
System.out.println(response.getHeaderString("Location"));
System.out.println(response.getStatus());


//get all items at resource /items
List<Item> items = target.request(MediaType.APPLICATION_JSON)
.get(new GenericType<List<Item>>() {
});
//note in above line we used GenericType
// cause readEntity(class) cannot handle the generics
System.out.println("all items: "+items);


}
}
Original Post




See Also