Try this:
package myco.mypkg;
import static org.junit.Assert.assertEquals;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.ServletDeploymentContext;
import org.glassfish.jersey.test.TestProperties;
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.Test;
import org.springframework.http.MediaType;
/**
* Simple tests, with a simple endpoint/resource, no injections, to debug and document how JerseyTest works.
*
*/
public class SimpleTest extends JerseyTest {
public SimpleTest() {
// Enable logging. This is really critical!
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
}
/** Start a Grizzly server */
@Override
protected TestContainerFactory getTestContainerFactory() {
return new GrizzlyWebTestContainerFactory();
}
/** Tell the server to deploy the TestResource endpoint class */
@Override
protected DeploymentContext configureDeployment() {
return ServletDeploymentContext.forServlet(new ServletContainer(new ResourceConfig(TestResource.class))).build();
}
/**
* Make a test call.
*/
@Test
public void testGet() {
WebTarget target = target("root");
String s = target.request().get(String.class);
assertEquals("GET", s);
}
@Test
public void testGetSub() {
WebTarget target = target("root/sub");
String s = target.request().get(String.class);
assertEquals("sub", s);
}
@Test
public void testPost() {
WebTarget target = target("root/mypost");
Form form = new Form();
form.param("id", "1234");
form.param("description", "test");
String s = target.request(MediaType.APPLICATION_FORM_URLENCODED_VALUE).post(Entity.form(form), String.class);
assertEquals("posted", s);
}
@Test
public void testPostWithParam() {
WebTarget target = target("root/postparams");
Form form = new Form();
form.param("id", "1234");
String s = target.request(MediaType.APPLICATION_FORM_URLENCODED_VALUE).post(Entity.form(form), String.class);
assertEquals("posted1234", s);
}
}
These calling the following:
package myco.mypkg;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
/**
* A test endpoint, for use with SimpleTest
*/
@Path("root")
public class TestResource {
@GET
public String get() {
return "GET";
}
@Path("sub")
@GET
public String getSub() {
return "sub";
}
@Path("mypost")
@POST
public String post() {
return "posted";
}
/** If you omit the @FormParam, id contains "id=1234" */
@Path("postparams")
@POST
public String post(@FormParam("id") String id) {
return "posted" + id;
}
}
I got the POST stuff by typing Entity. and waiting for the popup help to show me what the methods were.
I'm using Jersey 2.13.