Junit JerseyTest WebTarget POST支持

Junit JerseyTest WebTarget POST支持,junit,jersey,jackson,Junit,Jersey,Jackson,我正在开发一个轻量级的服务器应用程序,使用Jersey 2.12和Jackson 2实现了RESTful api 我正在编写测试,同时使用JUnit和JerseyTest进行开发。我知道我的Jersey资源工作正常,包括从JSON到JSON的编组,因为我使用PostMan Chrome插件手动测试了它们 基于中的示例,我的带有查询参数的GET测试也运行良好 下面是我想编写的一个简化的测试示例(我省略了样板代码以使想法更清晰): import static org.junit.Assert.ass

我正在开发一个轻量级的服务器应用程序,使用Jersey 2.12和Jackson 2实现了RESTful api

我正在编写测试,同时使用JUnit和JerseyTest进行开发。我知道我的Jersey资源工作正常,包括从JSON到JSON的编组,因为我使用PostMan Chrome插件手动测试了它们

基于中的示例,我的带有查询参数的GET测试也运行良好 下面是我想编写的一个简化的测试示例(我省略了样板代码以使想法更清晰):

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);
    }

}
我的基本问题是找不到任何关于如何正确使用WebTarget类进行post请求的文档。目标的WebTarget已正确构造,但行:

Response response = theTarget.request().post( Entity.entity(UserCredentialsBean.class, "application/json"));
不起作用


据我所知,WebTarget类在JerseyTest框架中是相当新的。有没有人能告诉我最近的文档、例子,或者在这里解释一下我是如何做到这一点的?

在我把我的问题发布到这里之前,我在谷歌上搜索了很多,但在查看之后,我的眼睛突然发现了这一点。我搜索了好几次,但都没有找到这个问题。不管怎样,我的问题的解决方案是:

我开始按照公认的答案中的解释实施,并很快使其生效

然后,我决定您应该可以完全避免使用JSON字符串表示,并且我已经做到了这一点

如果按照以下方式修改,则上述代码有效:

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是一个普通的Javabean。只有getter和setter以及默认构造函数

从JSON到JSON的编组由框架完成,moxy或jackson作为JSON提供程序