Running Your Tests?

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.

IntelliJ

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 '. It will then execute the test. It will also bring the 'Run' window into focus. So you can see the console and progress of your test. Or if the test is in focus you can press Fn + Shift + F10 (Mac).

Maven

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 Surefire Plugin

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.

Another thing people always want to do in run tests in Parallel. We can do by using the tags, by adding:

<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.