Do you know the difference between the maven-surefire-plugin and the maven-failsafe-plugin?
Twitter | LinkedIn | YouTube | Instagram
It’s not a coincidence that these plugins have synonymous names; both are dedicated to running tests. The key difference is in which part of the maven build lifecycle each one will run.
The default maven build lifecycle consists of the following phases:
- validate: validate the project is correct, and all necessary information is available.
- compile: compile the source code of the project.
- test: test the compiled source code using a suitable unit testing framework. These tests should not require the code to be packaged or deployed.
- package: take the compiled code and package it in its distributable format, such as a JAR.
- verify: run any checks on the results of integration tests to ensure quality criteria are met.
- install: install the package into the local repository for use as a dependency in other projects locally.
- deploy: done in the build environment, copies the final package to the remote repository for sharing with other developers and projects.
And by running any phase, all predecessors are also run. This means that to package your code, for example, maven will first validate, compile, and test it.
But bear in mind that it will not run all kinds of tests; it will only run Unit tests. If you want to run Integration tests, you must make sure you’re running the verify phase as well.
And this is where maven-surefire-plugin and maven-failsafe-plugin come into place. The surefire plugin is bound to the test phase. It is responsible for running all Unit tests. The failsafe plugin is usually bound to the verify phase and is responsible for running all Integration tests.
But the key difference is how they deal with test failures in a project. The Surefire plugin, used for simpler tests, stops the whole project build if there’s any mistake in these tests. This helps find and fix basic problems quickly. On the other hand, the Failsafe plugin, used for more complex tests that work with things like databases, doesn’t stop the build right away if there’s a mistake. Instead, it lets the build go on to finish up and clean up any resources used.
But how do they know which of your test classes are Unit or Integration tests, you may ask? Well, by default, the surefire plugin will detect all Java classes whose names either start with “Test” or end with “Test” or “TestCase.” While the failsafe plugin will detect all classes whose names either start with “IT” or end with “IT” or “ITCase.”
Separating Unit from Integration tests is a common practice, especially in big projects where the pipeline may take a long time to run. Since integration tests rely on actually running your application and interacting with external systems, they naturally take longer to run and stall the development process. Isn’t long-running pipelines the nightmare of every developer?
By separating those tests, you may configure your pipeline always to run unit tests and only run integration tests at specific times or right before the deployment phase.
By adopting this approach, you can catch simple issues in your code right away, prevent complex problems from reaching the deployment stage, and still maintain a smooth and enjoyable development process.
Pretty neat, right?
Contribute
Writing takes time and effort. I love writing and sharing knowledge, but I also have bills to pay. If you like my work, please, consider donating through Buy Me a Coffee: https://www.buymeacoffee.com/RaphaelDeLio
Or by sending me BitCoin: 1HjG7pmghg3Z8RATH4aiUWr156BGafJ6Zw
Follow Me on Social Media
Stay connected and dive deeper into the world of Maven with me! Follow my journey across all major social platforms for exclusive content, tips, and discussions.