Jersey rest测试由于资源方法内部的会话而失败 [英] Jersey rest test fails because of session inside of resource method

查看:107
本文介绍了Jersey rest测试由于资源方法内部的会话而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Jersey的休息api,但是当我尝试对其进行测试时,由于我在那里获取了会话数据而失败了,所以问题是,我该如何模拟或忽略这个Jersey无法检测到的会话变量? /p>

这是我的测试请求:

User response = target("/am/users/" + userId).request().get(new GenericType<User>() { });

这是我的资源:

@GET
@Path("{userId}")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public User getUser(@PathParam("userId") String userId, @Context HttpServletRequest request) {
    User supportUser = (User)request.getSession().getAttribute("USER"); // Here is where it fails.
    User user = userDao.getUser(userId, supportUser);
    return user;
}

解决方案

问题是Jersey测试不在servlet环境中运行,这是使用servlet API所必需的.如果您不知道,Jersey不需要在servlet容器中运行.如果使用 provider-grizzly2 ,如果您不使用未设置测试容器,它将默认运行GrizzlyTestContainerFactory,它仅启动Grizzly和HTTP服务器,而不启动servlet容器.

为了将JerseyTest配置为servlet容器,我们需要覆盖其他两个方法,configurDeploymentgetTestContainerFactory.对于后者,我们需要返回GrizzlyWebTestContainerFactory,这将设置servlet容器.在configureDeployment方法中,我们可以在servlet级别上配置应用程序.

public class ServletTest extends JersyTest {

    @Override
    public ResourceConfig configure() {
        // configure Jersey
    }

    @Override
    public TestContainerFactory getTestContainerFactory() {
        return new GrizzlyWebTestContainerFactory();
    }

    @Override
    public DeploymentContext configureDeployment() {
        return ServletDeploymentContext
                .forServlet(new ServletContainer(configure()))
                .build();
    }
}

如果使用的是provider-inmemory,则它不支持servlet部署,因此您将需要切换到码头供应商或灰熊供应商.

I have Jersey rest api, but when I try to test it it fails because of I am getting session data there, so the questions is, how can I mock or ignore this session variable, which Jersey can't detect?

Here is a request from my test:

User response = target("/am/users/" + userId).request().get(new GenericType<User>() { });

Here is my resource:

@GET
@Path("{userId}")
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
public User getUser(@PathParam("userId") String userId, @Context HttpServletRequest request) {
    User supportUser = (User)request.getSession().getAttribute("USER"); // Here is where it fails.
    User user = userDao.getUser(userId, supportUser);
    return user;
}

解决方案

The problem is that the Jersey test is not running inside a servlet environment, which is something that is required to work with the servlet APIs. If you are not aware, Jersey does not need to run inside a servlet container. If the case of using the provider-grizzly2, if you don't set up the test container, it will default to running the GrizzlyTestContainerFactory, which only starts Grizzly and an HTTP server, not a servlet container.

In order to configure the JerseyTest as a servlet container, we need to override two other methods, configurDeployment and getTestContainerFactory. With the latter, we need to return the GrizzlyWebTestContainerFactory, which will set up the servlet container. In the configureDeployment method, we can configure the application, at the servlet level.

public class ServletTest extends JersyTest {

    @Override
    public ResourceConfig configure() {
        // configure Jersey
    }

    @Override
    public TestContainerFactory getTestContainerFactory() {
        return new GrizzlyWebTestContainerFactory();
    }

    @Override
    public DeploymentContext configureDeployment() {
        return ServletDeploymentContext
                .forServlet(new ServletContainer(configure()))
                .build();
    }
}

If you are using the provider-inmemory, it doesn't support servlet deployment, so you will need to switch over to the jetty provider or the grizzly provider.

这篇关于Jersey rest测试由于资源方法内部的会话而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆