So we’ve written many tests, but how do we run them? Good question. We actually have many ways we can run our tests, so let’s explore them.
We can run tests directly from IntelliJ. Not a great solution for CI, but very good when creating the tests, and testing them. IntelliJ has a JUnit plugin, I believe it ships as part of IntelliJ, but you can check your have it by going to Preferences > Plugins and you should have one called JUnit. If it’s not there, you can google it and add it. Then all you need to do it right click on a test and click ‘Run
We can use Maven to run our tests by typing ‘mvn test’ in a console window while in the project folder. Now Maven is old school by default, it will only look for tests in class that are named Test*, *Test, *Tests or *TestCase. So if you try this and no tests run, that will be why. If it doesn’t pick up tests you’ll see this in the console
So if you follow the above naming convention then ‘mvn test’ has your covered. Otherwise, we have another way.
Maven has created the Surefire plugin. Now if using Maven and Java, I’d encourage you to use Surefire to run your tests. It provides a lot more control than just using default ‘mvn test’.
To install Surefire, you just need to add some XML to your POM file.
<build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> </plugin> </plugins> </pluginManagement> </build>
The first advantage of this is that we can inform Surefire of classes that contains tests, that don’t fit the naming convention, by adding this within the
<configuration> <includes> <include>Class1.java</include> <include>Class2.java</include> <include>Class3.java</include> </includes> </configuration>
Another thing people always want to do in run tests in Parallel. We can do by using the
<parallel>methods</parallel> <threadCount>10</threadCount> <!-- Number of tests to run in parallel. -->
I may add some more advanced posts on Surefire, but for now, I feel this is enough. I encourage you to check out the official documentation. Especially if you are looking to add your tests to a CI tools like Jenkins. You’re going to want to take advantage of the additional features.