users@jersey.java.net

[Jersey] Re: JerseyTests in one GrizzlyTestContainer

From: catch23 <lyc1116_at_foxmail.com>
Date: Tue, 28 Jan 2014 00:09:08 -0800 (PST)

ladislav.borak wrote
> Is possible that JerseyTest starts grizzlyTestContainer only once and
> before all test (@beforeClass) and not before every test(@before)?

Hi,this is my solution,create Junit @classrule to manually start or stop the
testcontainer.

public class JerseyTestRule implements TestRule{

        private TestContainerFactory tcf;
        
        private TestContainer tc;
        
        private Client c;
        
        private WebAppDescriptor wad;
        

        @Override
        public Statement apply(final Statement base, final Description des) {
                // TODO Auto-generated method stub
                tcf = new GrizzlyWebContainerFactory();
                wad = new WebAppDescriptor.Builder("com.example.test")
                .contextParam("contextConfigLocation",
                                "classpath:/applicationContext-test.xml")
                .servletClass(SpringServlet.class)
                .servletPath("/service/*")
                .initParam(
                                "com.sun.jersey.config.property.resourceConfigClass",
                                "com.sun.jersey.api.core.PackagesResourceConfig")
                .initParam("com.sun.jersey.config.property.packages",
                                "com.example.test")
                .initParam("com.sun.jersey.api.json.POJOMappingFeature", "true")
                .contextListenerClass(ContextLoaderListener.class)
                .requestListenerClass(RequestContextListener.class).build();
                tc = tcf.create(UriBuilder.fromUri("http://localhost/")
                .port(9998).build(), wad);
                
                return new Statement() {
                        @Override
                        public void evaluate() throws Throwable {
                                // TODO Auto-generated method stub
                                tc.start();
                                try {
                                        base.evaluate();
                                } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }finally{
                                        tc.stop();
                                }
                        }
                };
        }
}


public abstract class AbstractBaseTest {
         
        @ClassRule
        public static JerseyTestRule jerseyTestRule = new JerseyTestRule();

}



--
View this message in context: http://jersey.576304.n2.nabble.com/JerseyTests-in-one-GrizzlyTestContainer-tp7581822p7581873.html
Sent from the Jersey mailing list archive at Nabble.com.