0

我正在使用 JerseyTest 来测试 RESTful 网络服务。我观察到的一件事是 grizzly 是每种方法的启动/停止,这需要很长时间才能运行我的测试。有没有办法为整个测试班启动/停止灰熊一次。

public class JerseyTestBase extends JerseyTest {

@Override
protected TestContainerFactory getTestContainerFactory()
        throws TestContainerException {
    return new GrizzlyWebTestContainerFactory();
}

public JerseyTestBase() {

    super(new WebAppDescriptor.Builder("xxx.xxx.controller") 
    .contextPath("test") 
    .contextParam("contextConfigLocation", "classpath:spring/application-context.xml")             
    .contextListenerClass(ContextLoaderListener.class)
    .requestListenerClass(RequestContextListener.class)
    .servletPath("/*")
    .servletClass(SpringServlet.class)
    .initParam("JSONConfiguration.Builder.rootUnwrapping", "true")
    .initParam("com.sun.jersey.api.json.POJOMappingFeature", "false")
    .initParam("com.sun.jersey.config.property.MediaTypeMappings", "json :  pplication/json")
    .initParam("com.sun.jersey.spi.container.ContainerRequestFilters", "com.sun.jersey.api.container.filter.LoggingFilter")
    .initParam("com.sun.jersey.spi.container.ContainerResponseFilters", "com.sun.jersey.api.container.filter.LoggingFilter")
    .build());
}

}

实际测试班

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring/application-context.xml"/*,     "classpath:applicationContext-unittest.xml"*/})
public class PurchaseManagerClientControllerTestCase extends JerseyTestBase {}

提前致谢

4

2 回答 2

1

我认为目前无法直接使用JerseyTest,但是您可以基于JerseyTest和使用@BeforeClassand@AfterClass注释(来自org.junit包)而不是@Beforeand来创建自己的自定义父测试类@After

由这些注释的方法 must be static,因此您需要更多地重写原始类,但如果您不需要JerseyTest在使用多个TestContainer实现方面的多功能性,您可以创建自己的测试基类的简单得多的版本。基本上只需使用JerseyTest构造函数中的一些代码来初始化 Jersey 应用程序和GrizzlyTestContainerFactory.GrizzlyTestContainer类中的一些代码来启动/停止服务器等。

希望这可以帮助。

于 2013-10-01T15:01:20.633 回答
0

我知道这个问题已经很老了,但这是答案(我有完全相同的需求)

这个解决方案是在与泽西岛维护者讨论后提出的:https ://github.com/eclipse-ee4j/jersey/issues/4606

package org.opensilex.integration.test;

import java.net.URI;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.client.Client;
import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.DeploymentContext;
import org.glassfish.jersey.test.JerseyTest;
import static org.glassfish.jersey.test.JerseyTest.closeIfNotNull;
import org.glassfish.jersey.test.TestProperties;
import org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainer;
import org.glassfish.jersey.test.spi.TestContainerException;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.mockito.Mockito;

public abstract class SingleServerStartStopForTestClass extends JerseyTest {

    private static TestContainerFactory testContainerFactory;
    private static TestContainer globalTestContainer = null;
    private static Client globalClient = null;

    @AfterClass
    public static void stopGlobalContainer() throws Exception {
        try {
            if (globalTestContainer != null) {
                globalTestContainer.stop();
            }
        } finally {
            closeIfNotNull(globalClient);
        }
        globalTestContainer = null;
        globalClient = null;
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
        if (globalClient == null) {
            globalClient = this.getClient();
        }

    }

    @Override
    protected Client getClient() {
        if (globalClient == null) {
            return super.getClient();
        } else {
            return globalClient;
        }
    }

    @Override
    protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
        if (testContainerFactory == null) {
            testContainerFactory = new CustomTestContainerFactory();
        }
        return testContainerFactory;
    }

    private class CustomTestContainerFactory extends GrizzlyTestContainerFactory {

        @Override
        public TestContainer create(URI baseUri, DeploymentContext context) {
            if (globalTestContainer == null) {
                globalTestContainer = super.create(baseUri, context);
            }
            return globalTestContainer;
        }

    }

    @After
    @Override
    public void tearDown() throws Exception {
        // Overriden to avoid
    }

    @Override
    protected ResourceConfig configure() {

        enable(TestProperties.LOG_TRAFFIC);
        enable(TestProperties.DUMP_ENTITY);

        ResourceConfig resourceConfig;
        try {

            // initialize application
            resourceConfig = new ResourceConfig();

            // create a mock for HttpServletRequest which is not available with grizzly
            final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
            resourceConfig.register(new AbstractBinder() {
                @Override
                protected void configure() {
                    bind(request).to(HttpServletRequest.class);
                }
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return resourceConfig;
    }
}
于 2020-10-30T12:17:54.800 回答