JerseyTest WebTarget Website Support
I am developing a weight server application with RESTful api implemented with Jersey 2.12 and Jackson 2.
I am writing tests while developing using JUnit and JerseyTest. I know my Jersey resources are working as expected including sorting with JSON and JSON because I tested them manually using the PostMan Chrome plugin.
My GET tests with query parameters work well too, based on the example in the Jersey documentation Here is a simplified (I left out boilerplate code to make the idea clearer) example test I'd like to write:
import static org.junit.Assert.assertTrue;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import com.acme.api.rest.SessionsEndPoint;
import com.acme.api.rest.beans.UserCredentialsBean;
public class TestSession extends JerseyTest {
@Override
protected Application configure() {
return new ResourceConfig(SessionsEndPoint.class);
}
@Test
public void test() {
UserCredentialsBean userCredentialsBean = new UserCredentialsBean();
userCredentialsBean.setUserId("alice");
userCredentialsBean.setPassword("secret");
WebTarget theTarget = target("sessions/login");
Response response = theTarget.request().post( Entity.entity(UserCredentialsBean.class, "application/json"));
assertTrue(true);
}
}
The main problem is that I cannot find any documentation on how to properly use the WebTarget class for post requests. WebTarget theTarget is built correctly, but the line:
Response response = theTarget.request().post( Entity.entity(UserCredentialsBean.class, "application/json"));
does not work.
As I understand it, the WebTarget class is fairly new within JerseyTest. Is there anyone who can point me to any recent documentation, examples, or just explain how I can get this to work?
source to share
I did a lot of searches before posting my question here, but after checking back my eyes suddenly fell to this Related Question . I searched SO several times but never found this question. Anyway, here is the solution to my problem:
I started implementing as explained in the accepted answer and got it working quickly.
Then I decided that you should avoid using JSON string representations altogether, and I got this to work.
The above code works if modified like this:
import static org.junit.Assert.assertTrue;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import com.acme.api.rest.SessionsEndPoint;
import com.acme.api.rest.beans.UserCredentialsBean;
public class TestSession extends JerseyTest {
@Override
protected Application configure() {
return new ResourceConfig(SessionsEndPoint.class);
}
@Test
public void test() {
UserCredentialsBean userCredentialsBean = new UserCredentialsBean();
userCredentialsBean.setUserId("alice");
userCredentialsBean.setPassword("secret");
LoginResponseBean loginResponseBean =
target("sessions/login")
.request(MediaType.APPLICATION_JSON_TYPE)
.post(
Entity.entity(
userCredentialsBean,
MediaType.APPLICATION_JSON_TYPE
),
LoginResponseBean.class
);
assertTrue(
loginResponseBean.isSuccess()
&&
loginResponseBean.getToken().length()==36
);
}
}
LoginResponseBean is a simple Java Bean. Just getters and setters and a default constructor.
Marshalling to - and from JSON is executed by either moxy or jackson framework as the JSON provider.
source to share