Last active
May 17, 2016 11:36
-
-
Save psamsotha/d99d32b652e20685051c827e00f7b88e to your computer and use it in GitHub Desktop.
Spring Jersey Servlet Container Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
import javax.ws.rs.client.WebTarget; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.servlet.ServletContainer; | |
import org.glassfish.jersey.test.DeploymentContext; | |
import org.glassfish.jersey.test.JerseyTest; | |
import org.glassfish.jersey.test.ServletDeploymentContext; | |
import org.glassfish.jersey.test.grizzly.GrizzlyWebTestContainerFactory; | |
import org.glassfish.jersey.test.spi.TestContainerFactory; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.test.context.ActiveProfiles; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.test.context.web.WebAppConfiguration; | |
import org.springframework.web.context.ContextLoaderListener; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(locations = {"classpath:META-INF/applicationContext-test.xml", "classpath:META-INF/security-context.xml"}) | |
@ActiveProfiles("unit-test") | |
@WebAppConfiguration | |
public abstract class JerseySpringTest { | |
private JerseyTest _jerseyTest; | |
public final WebTarget target(final String path) { | |
return _jerseyTest.target(path); | |
} | |
@Before | |
public void setupJerseyTest() throws Exception { | |
_jerseyTest.setUp(); | |
} | |
@After | |
public void tearDownJerseyTest() throws Exception { | |
_jerseyTest.tearDown(); | |
} | |
@Autowired | |
public void setApplicationContext(final ApplicationContext context) { | |
_jerseyTest = new JerseyTest() { | |
@Override | |
protected ResourceConfig configure() { | |
ResourceConfig application = JerseySpringTest.this.configure(); | |
application.property("contextConfig", context); | |
return application; | |
} | |
@Override | |
public TestContainerFactory getTestContainerFactory() { | |
return new GrizzlyWebTestContainerFactory(); | |
} | |
@Override | |
public DeploymentContext configureDeployment() { | |
return ServletDeploymentContext | |
.forServlet(new ServletContainer(configure())) | |
.addFilter(CustomDelegatingFilterProxy.class, "springSecurityFilterChain") | |
.addListener(ContextLoaderListener.class) | |
.build(); | |
} | |
}; | |
} | |
protected abstract ResourceConfig configure(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment