13. Testing
Programming Project 2021/22

13.2. Getting Started with JUnit 5

JUnit 5

Setting up Maven

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.7.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.7.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
        </plugin>
    </plugins>
</build>

Creating your first test

Consider the following class.

public class Calculator {

   public static int sum(int value1, int value2) {
      return value1 + value2;
   }

   public static int multiply(int value1, int value2) {
      return value1 * value2;
   }

}

We can test it using this class.

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

class CalculatorTest {
  @Test
  void sum() {
    int result = Calculator.sum(1, 1);
    assertEquals(2, result);
  }

  @Test
  void sumNegative() {
    int result = Calculator.sum(-1, -1);
    assertEquals(-2, result);
  }

  @Test
  void multiply() {
    int result = Calculator.multiply(1, 1);
    assertEquals(1, result);
  }
}

Running your tests with your IDE

Most IDEs will provide support for you to run your tests through them.

Running your tests with Maven

In a Maven project, we can run tests by invoking the command below, which will trigger Maven to execute the default lifecycle all the way up to the Test phase.

mvn plugins package goals

If you execute on the root folder of your project:

$ mvn test

You should see something like this.

[INFO] Scanning for projects...
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------< org.example:Example1 >------------------------
[INFO] Building Example1 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Example1 ---
[INFO] Using 'UTF-8'  encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ Example1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Example1 ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/ozan/My/Current/Teaching/Programming_Project/Test/Testing/Example1/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ Example1 ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ Example1 ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running CalculatorTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.017 s - in CalculatorTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.678 s
[INFO] Finished at: 2023-04-17T17:25:01+02:00
[INFO] ------------------------------------------------------------------------

To run all tests within a class, do:

$ mvn -Dtest=CalculatorTest test

To run a single test, do:

$ mvn -Dtest=CalculatorTest#sum test

To run tests that match a pattern, do:

$ mvn '-Dtest=CalculatorTest#sum*' test