我正在使用JerseyTest来测试RESTful webservices。我观察到的一件事是灰熊是每个方法的开始/停止,这需要很长时间来运行我的测试。有没有办法在整个测试课上开始/停止灰熊。
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 {}
提前致谢
答案 0 :(得分:1)
我认为目前无法直接使用JerseyTest
,但您可以根据JerseyTest
创建自己的自定义父测试类,并使用 @BeforeClass
和 @AfterClass
注释(来自org.junit
包)而不是@Before
和@After
。
由这些方法注释的方法必须是static
,因此您需要更多地重写原始类,但如果在使用多个{JerseyTest
方面不需要TestContainer
的多功能性1}}实现,您可以创建自己的测试基类的简单版本。
基本上只需使用JerseyTest
构造函数中的一些代码来初始化Jersey应用程序和来自GrizzlyTestContainerFactory.GrizzlyTestContainer
类的一些代码来启动/停止服务器等。
希望这有帮助。
答案 1 :(得分: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;
}
}