I’d suggest using failsafe plugin for integration/end2end tests and surefire for unit tests. I find this kind of separation very handy:
failsafe provides hooks for starting/stopping external resources (like tomcat/jetty) and it runs all *IT.java test classes out-of-box. as surefire runs only *Test.java tests.
I would have used failsafe if I had a need for tearing down things after a failure. But that would have meant that I would have needed more setup. My goal with this blog was to justify my choice on how I usually separate unit and integration tests and I wanted as little configuration as possible.
There is also another approach – use TestNG and it’s test groups feature.
Then, you may categorise each test to specific group (e.x. UNIT, STATIC_ANALYSIS, INTEGRATION) and configure surefire plugin to have separated execution for each test.
+ No need to follow specific naming convention pattern for tests,
+ No need to put tests in specific packages
+ No need to build and install production code artifact prior to running integration tests (when using separated module for integration tests)
– Need to use TestNG, buy for me it’s rather an advantage
– Need to remember about adding groups=X to your @Test annotation, but it may be done by one test class which gathers all *Test.java files and checks for that.
Code snippet for configuring maven-surefire-plugin:
Then, you may specify another profile which can be enabled only on your CI machine:
🙂 My experience with TestNG (which I like very much, feature-wise) is that the separation is really easy: with TestNG all tests are slow – at least in Eclipse. It might be due to the TestNG runner/eclipse plugin, but it makes TestNG a nogo for me.
Comment by Anonymous Coward — May 22, 2014 @ 14:38
I wouldn’t know anything about the performance in Eclipse. Life is too short for bad tooling so I use IntelliJ IDEA. The free version is great. If you can afford it, support the developers by buying the commercial version.
I don’t think so. The code in the example has been executed without any problems. I have a Maven plugin that reads my source code and then includes it into my text. I don’t do any retyping or cut and paste of the source code. What is the error you get when you try to execute it?
Thanks a lot for all your great articles!
I just have a question about the pom.xml of the test module. According to the “Maven by example” book, which you referred to, a maven module should refer to its parent by means of a element. The pom.xml of your test module does not. Isn’t that an error?
Comment by Mikko Östlund — November 13, 2014 @ 01:28
It could be an error if you want the test module to be aware of a parent. In this case, I didn’t want the test module to be aware of any parent so I didn’t specify any parent.
Specifying a parent is optional. A use case where you would like a module to be aware of its parents could be when you have a some common specifications that all modules should be aware of. A specific version of a dependency could be an example. In that case, you could have a dependency management section in the parent where you specify a dependency with version and in each sub module you just specify the dependency without version. This would allow you to update the version in one place and not many places.