使用jersey测试框架时,Servlet上下文注入失败

时间:2010-12-02 12:16:38

标签: java testing jersey freemarker servlets

我开始使用球衣,并尝试使用TDD让freemarker使用它。我想为我的模板创建一个ViewProcessor,但是无法在类中注入servlet上下文。

这是类代码:

@Provider
public class myProcessor implements ViewProcessor<Template> {

    [...]

    @Context
    public ServletContext myContext;

    [...]

 freemarkerConfiguration.setTemplateLoader(
       new WebappTemplateLoader(myContext,
           myContext.getInitParameter("freemarker.template.path")));

    [...]
    }

这是测试代码:

public class myProcessorTest extends JerseyTest {

    public static myProcessor mp;

    public myProcessorTest() throws Exception{
        super(new WebAppDescriptor.Builder("com.domain").build());
    }

    @Test
    public void firstTest(){
        mp = new myProcessor();
        String path = new String("test.ftl");
        Template template = mp.resolve(path);
        assertNotNull(template);
    }
}

我使用maven和依赖关系如下:

<dependency>
    <groupId>com.sun.jersey.jersey-test-framework</groupId>
    <artifactId>jersey-test-framework-grizzly</artifactId>
    <version>1.5-SNAPSHOT</version>
    <scope>test</scope>
</dependency>

当我部署到我的本地jetty服务器时,我的代码运行正常。但是如果我想在我的IDE中测试代码,那么当我运行测试时,它无法注入servlet上下文(@Context):myContextnull:/

我想我错过了一些东西,但我是servlet世界的初学者。

2 个答案:

答案 0 :(得分:0)

有几种方法可以做到这一点。删除构造函数并实现这样的configure()方法:

public class myProcessorTest extends JerseyTest {

    public static myProcessor mp;

    @Override
    protected AppDescriptor configure() {
    return new WebAppDescriptor.Builder("com.domain")
        .contextParam("contextConfigLocation", "classpath:/applicationContext.xml")
        .contextPath("/").servletClass(SpringServlet.class)
        .contextListenerClass(ContextLoaderListener.class)
        .requestListenerClass(RequestContextListener.class)
        .build();
  }

或者您也可以使用spring上下文注释您的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class MyProcessorTest extends JerseyTest {

  public static myProcessor mp;

答案 1 :(得分:0)

假设您正在使用默认/标准Grizzy2测试框架提供程序,那么这个问题的解决方案不需要spring。根据{{​​3}},jersey-test-framework-provider-grizzly2框架提供程序在构造应用程序上下文时不使用servlet环境。您的症状是由于没有ServletContext实例注射而导致的。

解决方法是自己为单元测试提供测试容器。首先,修改依赖项:

<!--<dependency>
    <groupId>org.glassfish.jersey.test-framework.providers</groupId>
    <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
    <version>2.25</version>
    <scope>test</scope>
</dependency>-->
<dependency>
    <groupId>org.glassfish.jersey.test-framework</groupId>
    <artifactId>jersey-test-framework-core</artifactId>
    <version>2.25</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-grizzly2-servlet</artifactId>
    <version>2.25</version>
</dependency>

然后,修改您的测试以提供Grizzy servlet容器:

@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
  return (final URI baseUri, final DeploymentContext deploymentContext) -> 
    new TestContainer() {
    private HttpServer server = null;

    @Override
    public ClientConfig getClientConfig() {
      return null;
    }

    @Override
    public URI getBaseUri() {
      return baseUri;
    }

    @Override
    public void start() {
      try {
        this.server = GrizzlyWebContainerFactory.create(baseUri, Collections
            .singletonMap("jersey.config.server.provider.packages", "<your-package-name>"));
      } catch (final ProcessingException | IOException cause) {
        throw new TestContainerException(cause);
      }
    }

    @Override
    public void stop() {
      this.server.shutdownNow();
    }
  };
}

我假设您将在多个单元测试中使用它,因此扩展JerseyTest可能是明智的,因此可以自动执行此常见配置。此外,可能值得查看this answer以查看测试容器是否提供了您希望模拟/保留的任何功能。提供的示例应该可以放入您的测试中,以至少确认这是一个修复。

编辑:在我自己的实现中,我需要能够在生成服务器时仍然提供ResourceConfig。我怀疑这可能是其他Jersey Test Framework用户的常见情况。随后是TestContainerFactory的一个工作示例。

import java.io.IOException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletContext;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.core.UriBuilder;

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.servlet.WebappContext;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.spi.TestContainer;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.glassfish.jersey.test.spi.TestHelper;


public class RestTestContainerFactory implements TestContainerFactory {      
  public static class RestTestContainer implements TestContainer {
    private static final Logger LOGGER = Logger.getLogger(RestTestContainer.class.getName());

    private URI baseUri = null;
    private final HttpServer server;

    public RestTestContainer(final URI baseUri, final DeploymentContext context) {
      this.baseUri = UriBuilder.fromUri(baseUri).path(context.getContextPath()).build();
      if(LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("Creating RestRestContainer configured at the base URI "+TestHelper.zeroPortToAvailablePort(baseUri));
      }

      try {
        final WebappContext webContext = new WebappContext("TestContext", context.getContextPath());
        context.getResourceConfig()
               .register(new AbstractBinder() {
                @Override
                protected void configure() {
                  bind(webContext).to(ServletContext.class);
                }
              });
        this.server = GrizzlyHttpServerFactory.createHttpServer(this.baseUri, context.getResourceConfig(), false);
        webContext.deploy(this.server);

      } catch (final ProcessingException cause) {
        throw new TestContainerException(cause);
      }
    }

    @Override
    public ClientConfig getClientConfig() {
      return null;
    }

    @Override
    public URI getBaseUri() {
      return baseUri;
    }

    @Override
    public void start() {
      if(server.isStarted()) {
        LOGGER.warning("Ignoring start request - RestTestContainer is already started");
      } else {
        LOGGER.fine("Starting RestTestContainer...");
        try {
          server.start();
          if(baseUri.getPort() == 0) {
            baseUri = UriBuilder.fromUri(baseUri)
                .port(server.getListener("grizzly").getPort())
                .build();
            LOGGER.info("Started GrizzlyTestContainer at the base URI "+baseUri);
          }
        }
        catch(final ProcessingException | IOException cause) {
          throw new TestContainerException(cause);
        }
      }
    }

    @Override
    public void stop() {
      if(server.isStarted()) {
        LOGGER.fine("Stopping RestTestContainer...");
        server.shutdownNow();
      } else {
        LOGGER.warning("Ignoring stop request - RestTestContainer is already stopped");
      }
    }
  }

  @Override
  public TestContainer create(final URI baseUri, final DeploymentContext context) {
    return new RestTestContainer(baseUri,context);
  }
}

令人沮丧的是,灰熊的GrizzlyWebContainerFactory将提供servlet上下文,但不配置资源配置。相反,GrizzlyHttpServerFactory会使用ResourceConfig配置应用程序,但不会提供Web上下文。

我们可以通过手动创建WebappContext(扩展ServletContext),配置它,然后通过AbstractBinder将其注入资源配置来解决此问题。