Nerd For Tech
Published in

Nerd For Tech

Unit Testing with JUnit5 in IntelliJ Maven Project [Software Development Engineer in Test Article Series Part 9]

Up to now, I have written 8 articles which are mostly about the structure of software testing. The articles from the 8th one are technical articles. You can read the previous articles from here.

This article will handle JUnit5 (Jupiter) for just beginners. There are more features of JUnit that are not discussed in this article. So you can start with this article and add on by searching on the web.

I assume that the reader knows coding with java, and is familiar with IntelliJ IDE.

Technical prerequisites:

  • IntelliJ IDE must be installed.
  • JDK must be installed. (At least Java 11 is essential)

What is JUnit?

JUnit is a popular unit-testing framework for Java.

What is unit-testing?

Unit testing is a type of software testing. In unit-testing, as it can be understood from its name, units of a source code, like methods, classes, components are tested.

Who performs unit testing?

Mostly developers. Because, you have to have access to the actual source code that is written by developers. But, in some cases, SDETs (Software Development Engineer in Test) can perform unit testing, too.

Why Junit? Are there any testing tools for doing unit tests over Java code?

Yes, there are. We also have TestNG for Java unit tests. Actually, TestNG has more options than Junit. We will talk about it later. Since the main logic is the same, I will firstly handle JUnit. There is no harm to know one more test tool.

Which version of Junit will we use?

We will use Junit5, in other words Junit Jupiter. It has new features over Junit4.

How to use JUnit?

First, we will create a Maven project in IntelliJ. The important thing is you have to use Java 11 for Junit5. Let’s follow the steps:

Open IntelliJ>Click New Project
  1. select Maven as project type
  2. You must have Java 11
  3. If you do not have Java 11, download from here and come to this step again.
Select Maven and Java 11 as SDK>click next
Name your project and click finish
Default project view and pom.xml file content

Now we will add our JUnit dependencies. The first one will be JUnit API, the other will be JUnit Engine. So open your internet browser and go to as shown below:

mvnrepository home page
search for “junit jupiter”

We will select the first one now. Once we add it , then we will come back to this page and select the second one. So click first one now:

select 5.7.0
(1) select Maven and (2) click inside the box, it will copy the content

Once you click it will automatically copy the content, if not, copy the box content manually.

Now, we will add the copied content to our pom.xml file. But first we need to add “dependencies” tag with its closing pair as shown below:

add dependencies tag

Then, we can paste the copied content just between these tags as shown below:

paste the content in the dependencies tag

Now, go 2 times back to access the other JUnit dependency (Engine). Reach the page shown below:

select JUnit Jupiter Engine
select 5.7.0
(1) select Maven and (2) click the content of the box

Once you click it will automatically copy the content, if not, copy the box content manually.

(1) paste the copied content just 1 line after the previous one

Now, we must update the dependencies, so that Maven can fetch these two packages from the repo. We can do it by clicking the refresh icon (2), or by opening the Maven side window (3) and clicking the update button.

After refreshing, at the end, there must be no red line and no error in this pom.xml file. If so, close it, if not, check the previous steps.

Now, we will add a class to our src/main/java folder, as shown below:

add new class to the src/main/java folder
name your class
write a simple add method in that class

Now, we will add a test class to test this method. Remember, unit testing is just source code testing by units. This method is a small unit of our program.

We will add the test class to test/java folder, as shown below:

add test class to test/java folder
name your test class
our first test method
  1. Import Junit packages that we will use
  2. Create a test method with “@Test” annotation
  3. We need two int parameters for the “add” method in “MyCalculator” class. So we define two int variables and initialize them.
  4. Since we define test results (FAIL or PASS) by comparing the expected and actual results, we calculate the expected result.
  5. The actual result is, of course, the add method’s result.
  6. The last step is to compare expected results and actual results. The “assertEquals” method of JUnit “Assertions” class will perform this comparison. If they are equal the test will PASS, if not the test will FAIL.

Let’s run this test by clicking the green arrow near the method name, as shown below:

(1) click green arrow, then (2) click the second green arrow
test result of addMethodTest()

As we see, the test is finished and passed. So we ran our first JUnit test successfully.

Let’s do another practice:

add a new method to MyCalculator class
add a test for multiply method

As we did in the previous test method, we defined variables to use while calling the “multiply” method. Moreover, we created an expected result based on these variables. The final step is to compare expected and actual results. We do it by the “assertEquals” method. Let’s run this test, as shown above:

(1) click the green arrow, then (2) click the second green arrow
test result of multiplyMethodTest()

As we see, the test failed. In the right window “expected” and “actual” results are shown. Since they are not equal the test failed.

We did it intentionally to show a failing test. The reason why it failed is, parameters are divided in the “multiply” method instead of multiplying.


So we detected the bug.

Let’s run both tests together without correcting the code. Code correction is not SDET’s duty. It can not. Run the tests in class level, as shown above:

(1) click the green arrow, then (2) click the second green arrow
class level test results

As we see, “addMethodTest” is PASSED, but “multiplyMethodTest” is FAILED. We also can inspect the expected and actual results.

I am impressed. It is easy to use and understand. What else can JUnit do?

Yes, it is very easy to implement JUnit. I will talk about some JUnit features briefly. Because explaining all of them needs time. Maybe we can discuss them in new articles.

  • You can define a method to run before each test method (it is used for preparing variables and resources)
  • You can define a method to run after each test method (it is used for releasing the resources)
  • You can define a “Display Name” for your methods to write its name in an understandable way in the console or in the reports.
  • You can repeat a test multiple times.
  • You can use different assertions like assertEquals, assertTrue, assertFalse, assertNotNull, assertNull.
  • You can test exceptions if they are thrown or not.
  • You can test multiple assertions together, without stopping the test for failed assertions.
  • You can define timeouts for your tests.
  • You can disable a test without deleting or commenting on the test code.
  • You can write dynamic and parameterized tests.
  • You can define different data sources.
  • You can write nested tests.
  • You can create temporary files and paths easily.
  • You can create test suites.
  • You can change the test methods execution order.
  • You can use JUnit with Gradle and Maven projects.

I explained the JUnit usage with IntelliJ, but the same structure can be implemented in Visual Code and Eclipse as well.

That is enough for now. Let’s meet in new articles.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store