Separate and rule your tests

Everyone who cares about quality of his code must write tests. Would be better to write tests before code (I mean TDD), but presence at least some kind of tests are already well. Tests are the best way to prove that your code works as well as you expect. Also your tests are the best documentation not only how to use your code, but also how you understand a task.

But there is some problem - people usually skip tests. Why? There could be many reasons, but, in my view, the most popular problem is tests take a lot of time. What can we do? First of all we should separate unit tests from integration tests. The latter are slow, because they usually require getting a context and interaction with third-party services unlike unit tests. This kind of tests are very fast, because they check only short piece of code that you want, and also they use mock objects instead of third-party services or libraries. To solve this problem we will use two maven plugins. The first of these is maven-surefire-plugin. I am sure you have ever heard about it. Let’s look at the configuration.

We indicated that all files that end with "*" are unit tests. They will be executed on test phase which developers should run after each code change.

Next plugin will be responsible for running integration tests. It is maven-failsafe-plugin. Its setup as simple as the previous.

This configuration almost like previous plugin configuration except two things. At first we take into account only files, that end with “*”. Secondly, at this time we need to specify that “integration-tests” and “verify” goals will be executed on “verify” building phase.

So, it is the easiest way to make our development more comfortable. Now for checking our code we need to run just “mvn test”. If we want to run integration tests enough to type “mvn verify”. Note that unit tests also will be run.


Popular posts from this blog

JEEConf 2017 How it was

Kata для мозга или определяем день релиза правильно

Java 8 vs GoF: Strategy