A short while back I started to work with Ruby on Rails. Incredible fun and inspiring and one of the (many) things I like is the distinction Rails by default creates of the different levels of test scopes. It has separate folders to store unit, functional and performance tests. I still also do Java projects.
Now in Java – and using the Maven default folder layout – you get one folder for storing your production code (src/main/java) and one for your test code (src/test/java). That got me to thinking to apply Rails’ way to structure your Java test suite.
Please please, gimme the good stuff!
Yeah yeah, getting there. So, what you need to be doing is the following:
- Under the default
src/test/javafolder create a folder per test level you want. I have separated folders for unit, functional and integration tests - Alter your Maven pom.xml to contain the following:
- Now you can add test classes into the appropriate location. If you run
mvn testthey will be contained in the test run. Also you can get Eclipse to understand this separation. Just runningmvn eclipse:eclipsewill get you the following:
...
<build>
<testSourceDirectory>src/test/java/unit</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/test/java/functional</source>
<source>src/test/java/integration</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...Conclusion
Cramming everything in a single test folder can get really messy. From the outside it’s hard to see what the test scope of a class is. Sometimes you see this scope included in the class name, or even in the package definition. I don’t like that all too much. Now it’s immediately clear what the scope of your tests is by looking at the folder they’re in. I got some positive responses from different people. Hope this helps you too, let me know what you think!



