Rendez vous au Devoxx France 2013

Si vous êtes un peu geek (ça doit être le cas, car vous êtes sur notre blog :)) et si vous surfez sur le web pendant votre temps de travail (?? :) ), vous savez certainement que le Devoxx France aura lieu le 27 mars prochain. Et comme pour la 1ère édition, FastConnect fait de nouveau parti des sponsors de cet événement.

Et une fois de plus, nos consultants se plient en 4 (ou en 2 .. ça dépend de l’agilité de chacun :)) pour vous préparer une petite surprise ici.

Donc rendez vous sur notre stand ou sur cette page à partir du 27 mars.

On vous attend ! Comment ? Vous n’y allez pas ? Oh .. vous allez nous manquer :(…. Mais rassurez-vous, vous pouvez toujours venir nous voir sur notre stand au BreizhCamp 2013, une conférence chez les bretons pour les bretons et les non bretons dont FastConnect est également sponsor.

A bientôt…

Embedding Tomcat 7 for unit tests

Recently (in fact, few months ago), I have had to develop a small REST service exposing some methods that could be invoked remotely by a REST client (using GET and POST methods). The service contract was so simple that I took less time to write the service class by using Jersey that declaring all the Spring dependencies in the project pom.xml or creating a new application with the Play! framework.

As a not fully agile developer, I didn’t write the tests before starting the service (oh !! you don’t respect the TDD ? no ! I don’t — unless for this time — :)). So after finishing the few lines of code of my service (<500 lines), came the time to test my service.

Then I was wondering myself how could make quickly an integration test in my JUnit test case inside my IDE, before creating a new Jenkins Job (yes ! this time I’ve heard about the CI :)). Below some of them :

  • Servlet Unit test
  • using Arquilian
  • starting manually an external servlet container
  • starting a container with Cargo
  • using an embedded server started during the test setup phase (@Before for instance)
  • etc…

As a lazzy developer as usual, I opted for the simpliest choice (for me) : using an embedded Tomcat server.

First, I added the following line (dependencies management) in my project pom.xml. Yes! here the project is a Maven based project.


<properties>
	<tomcat-version>7.0.26</tomcat-version>
</properties>
...
<!-- TEST -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-core</artifactId>
			<version>${tomcat-version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-logging-juli</artifactId>
			<version>${tomcat-version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<version>${tomcat-version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-jasper</artifactId>
			<version>${tomcat-version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-jasper-el</artifactId>
			<version>${tomcat-version}</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.apache.tomcat</groupId>
			<artifactId>tomcat-jsp-api</artifactId>
			<version>${tomcat-version}</version>
			<scope>test</scope>
		</dependency>

And then, here comes the code… :)

Please note that there are so many different ways to achieve the same goal, therefore my following code is provided  for the example purpose only. Of course, you can write your classes by using your own style :)

First, I wrapped the Tomcat inside a Runnable that will be started during the test init phase

//import are removed
public class EmbeddedServer implements Runnable {

	private Tomcat	tomcat;
	private Thread	serverThread;

	public EmbeddedServer(int port, String contextPath) throws ServletException {
		tomcat = new Tomcat();
		tomcat.setPort(port);
		tomcat.setBaseDir("target/tomcat");
		tomcat.addWebapp(contextPath, new File("src/main/webapp").getAbsolutePath());
		serverThread = new Thread(this);

	}

	public void start() {
		serverThread.start();
	}

	public void run() {
		try {
			tomcat.start();
		} catch (LifecycleException e) {
			throw new RuntimeException(e);
		}
		tomcat.getServer().await();
	}

	public void stop() {
		try {
			tomcat.stop();
			tomcat.destroy();
			deleteDirectory(new File("target/tomcat/"));
		} catch (LifecycleException e) {
			throw new RuntimeException(e);
		}
	}

	void deleteDirectory(File path) {
		if (path == null) return;
		if (path.exists()) {
			for (File f : path.listFiles()) {
				if (f.isDirectory()) {
					deleteDirectory(f);
					f.delete();
				} else {
					f.delete();
				}
			}
			path.delete();
		}
	}

Finally run my JUnit tests where my client could request my REST service that has been previously deployed on the embedded Tomcat instance

public class MyAppDeployedOnLocalTest {

	@Before
	// or @BeforeClass
	public void startServer() throws ServletException {
		embeddedServer = new EmbeddedServer(9090, "/myservice");
		embeddedServer.start();

	}

	@After
	// or @AfterClass
	public void stopServer() {
		embeddedServer.stop();
	}

	public void test_send_request_without_parameters (){
            Client client = ...
            Reply reply = client.sendRequest();
            assertTrue (reply.isOk());
    }
}

And ..voilà ! :)