Aqua 2024.3 Help

JUnit 5

In this tutorial, you will learn how to set up JUnit for your projects, create tests, and run them to see if your code is operating correctly. It contains just the basic steps to get you started.

If you want to know more about JUnit, refer to the official documentation.

You can choose to follow the tutorial using either Maven, Gradle, or the IntelliJ builder.

Create a project

  1. In the main menu, go to File | New | Project.

  2. In the New Project wizard, select Java from the list on the left.

  3. Specify the name for the project, for example, junit-tutorial, and select Maven as a build tool.

  4. From the JDK list, select the JDK that you want to use in your project.

    If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory.

    If you don't have the necessary JDK on your computer, select Download JDK.

  5. Click Create.

Add dependencies

For our project to use JUnit features, we need to add JUnit as a dependency.

  1. Open pom.xml in the root directory of your project.

  2. In pom.xml, press Alt+Insert and select Dependency.

  3. In the dialog that opens, type org.junit.jupiter:junit-jupiter in the search field.

    Locate the necessary dependency in the search results and click Add.

  4. When the dependency is added to pom.xml, press Ctrl+Shift+O or click Reimport All Maven Projects in the Maven tool window to import the changes.

Create a project

  1. In the main menu, go to File | New | Project.

  2. In the New Project wizard, select Java from the list on the left.

  3. Specify the name for the project, for example, junit-tutorial, and select Gradle as a build tool.

  4. From the JDK list, select the JDK that you want to use in your project.

    If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory.

    If you don't have the necessary JDK on your computer, select Download JDK.

  5. Click Create.

Add dependencies

For our project to use JUnit features, we need to add JUnit as a dependency.

  1. Open build.gradle in the root directory of your project.

  2. In build.gradle, press Alt+Insert and select Add Maven artifact dependency.

  3. In the tool window that opens, type org.junit.jupiter:junit-jupiter in the search field.

    Locate the necessary dependency in the search results and click Add.

  4. When the dependency is added to build.gradle, press Ctrl+Shift+O or click Reimport All Gradle Projects in the Gradle tool window to import the changes.

Create a project

  1. In the main menu, go to File | New | Project.

  2. In the New Project wizard, select Java from the list on the left.

  3. Specify the name for the project, for example, junit-tutorial, and select IntelliJ as a build tool.

  4. From the JDK list, select the JDK that you want to use in your project.

    If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory.

    If you don't have the necessary JDK on your computer, select Download JDK.

  5. Click Create.

Add dependencies

For our project to use JUnit features, we need to add JUnit as a dependency.

  1. In the main menu, go to File | Project Structure (Ctrl+Alt+Shift+S).

  2. Under Project Settings, select Libraries and click the New Project Library button | From Maven.

  3. In the dialog that opens, specify the necessary library artifact, for example: org.junit.jupiter:junit-jupiter:5.9.1.

  4. Apply the changes and close the dialog.

    Manually adding a testing library to a project

Write application code

Let's add some code that we'll be testing.

  1. In the Project tool window Alt+1, go to src/main/java and create a Java file called Calculator.java.

  2. Paste the following code in the file:

    import java.util.stream.DoubleStream; public class Calculator { static double add(double... operands) { return DoubleStream.of(operands) .sum(); } static double multiply(double... operands) { return DoubleStream.of(operands) .reduce(1, (a, b) -> a * b); } }

Create tests

Now let's create a test. A test is a piece of code whose function is to check if another piece of code is operating correctly. In order to do the check, it calls the tested method and compares the result with the predefined expected result. An expected result can be, for example, a specific return value or an exception.

  1. Place the caret at the Calculator class declaration and press Alt+Enter. Alternatively, right-click it and select Show Context Actions. From the menu, select Create test.

    The code of the class that we are going to test
  2. Select the two class methods that we are going to test.

    The Create Test dialog
  3. The editor takes you to the newly created test class. Modify the add() test as follows:

    @Test @DisplayName("Add two numbers") void add() { assertEquals(4, Calculator.add(2, 2)); }

    This simple test will check if our method correctly adds 2 and 2. The @DisplayName annotation specifies a more convenient and informative name for the test.

  4. Now what if you want to add multiple assertions in a single test and execute all of them regardless of whether some of them fail? Let's do it for the multiply() method:

    @Test @DisplayName("Multiply two numbers") void multiply() { assertAll(() -> assertEquals(4, Calculator.multiply(2, 2)), () -> assertEquals(-4, Calculator.multiply(2, -2)), () -> assertEquals(4, Calculator.multiply(-2, -2)), () -> assertEquals(0, Calculator.multiply(1, 0))); }

    The assertAll() method takes a series of assertions in form of lambda expressions and ensures all of them are checked. This is more convenient than having multiple single assertions because you will always see a granular result rather than the result of the entire test.

Run tests and view their results

After we have set up the code for the testing, we can run the tests and find out if the tested methods are working correctly.

  • To run an individual test, click in the gutter and select Run.

    The popup that appears after clicking on the Run icon in the gutter
  • To run all tests in a test class, click against the test class declaration and select Run.

You can view test results in the Run tool window.

The results of the tests shown in the Run tool window
Last modified: 24 October 2024