One of the best solutions to do it, in my opinion, is running your application on a Jetty server that is embedded in your unit test with some in-memory database.
But let's do it step-by-step:
Step1: Start Jetty from a Unit Test
(In this guide I have used TestNG, but I see no reason why the same functionality cannot be achieved in JUnit)So first we need to start Jetty in our test. But before we actually do that, we need to make sure that our Maven project contains the relevant dependencies. So first we need Jetty:
<dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>${jetty-version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>${jetty-version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-jndi</artifactId> <version>${jetty-version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-plus</artifactId> <version>${jetty-version}</version> <scope>test</scope> </dependency>
The Jetty dependencies in this guide already contains the JNDI support. It will be needed later. But if JNDI support is not required, they can be omitted.
And then let's start it before the tests start:
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.xml.XmlConfiguration; import org.testng.annotations.BeforeClass; public class MyTest { private static final String RESOURCES_URL = "/rs"; private static final String CONTEXT = "/app_context"; private static final String DS_CONFIG = "/jetty-ds-test.xml"; private String baseResourceUrl; @BeforeClass public void startJetty() throws Exception { Server server = new Server(0); // see notice 1 server.setHandler(new WebAppContext("src/main/webapp", CONTEXT)); // see notice 2 // see notice 3 InputStream jettyConfFile = InboxTest.class.getResourceAsStream(DS_CONFIG); if (jettyConfFile == null) { throw new FileNotFoundException(DS_CONFIG); } XmlConfiguration config = new XmlConfiguration(jettyConfFile); config.configure(server); server.start(); // see notice 1 int actualPort = server.getConnectors()[0].getLocalPort(); baseResourceUrl = "http://localhost:" + actualPort + CONTEXT + RESOURCES_URL; }
1. Jetty is started on a random port. The actual url with the actual port is saved to baseResourceUrl to be used later by tests.
2. Web application context points to maven's src/main/webapp.
3. Jetty is started with a data source configuration. (See Runnig Jetty from Maven using JNDI Data Source)
Part 2
Recommended Reading
1. Next Generation Java Testing: TestNG and Advanced Concepts2. Apache Maven 3 Cookbook
3. Spring Recipes: A Problem-Solution Approach
1 comment:
Post a Comment