Getting started with JUnit Assertions

In this tutorial we will learn how to use JUnit assertions. An assertion is one of a number of static methods on the org.junit.jupiter.api.Assertions class. Assertions are used to test a condition that must evaluate to true in order for the test to continue executing. If an assertion fails, the test is halted at the … Read more

Getting started with JUnit and IntelliJ Idea

In this tutorial we will learn how to create a JUnit 5 project using IntelliJ. We will be at first creating a simple Maven Project, then we will add a basic Class and a JUnit Test for it. Creating an IntelliJ Project: Requirements: Download IntelliJ Community edition from: Install IntelliJ Idea, then launch it: … Read more

How to control JUnit Tests order

JUnit tests are executed using a deterministic (but not obvious) criteria, however it’s possible to define the exact order of Test execution. This tutorial shows how to do it. Even if true unit tests should not rely on a precise order in which they are executed, sometimes it is needed to enforce a specific order … Read more

How to verify the Console output in JUnit Tests

This tutorial shows how to assert the Console output of a JUnit Test: First of all we need a class which sets the out and err streams to the PrintStream in the @Before and @After callback methods: private final ByteArrayOutputStream out = new ByteArrayOutputStream(); private final ByteArrayOutputStream err = new ByteArrayOutputStream(); private final PrintStream originalOut … Read more

Getting started with JUnit assumptions

Assumptions are similar to assertions, except that assumptions must hold true or the test will be aborted. In contrast, when an assertion fails, the test is considered to have failed. JUnit Assumptions helps us in skipping a test for some specific scenario. Sometimes our test cases are dependent on the inputs, operating systems or any … Read more

How to Tag Tests in JUnit 5

In this tutorial, we are going to learn how to Tag Tests using JUnit Jupiter tests. Test classes and methods can be tagged in the JUnit 5 programming model by means of the annotation @Tag. Those tags can later be used to filter test discovery and execution. In this example, we see the use of … Read more

Managing JUnit Test Timeout

If you want to ensure that a test fails if it isn’t done in a certain amount of time you can use the assertTimeout() method. This ensures that tests that take too long, can be automatically failed. Here is an example package com.example; import static org.junit.Assert.assertEquals; import static java.time.Duration.ofMillis; import static java.time.Duration.ofMinutes; import static org.junit.jupiter.api.Assertions.assertTimeout; … Read more

How to run a single test in Maven

How to run a single Class Test with Maven Open a terminal and from the Maven project directory (which contains a valid pom.xml file) you can execute the following command: mvn -Dtest=ExampleTest test where ExampleTest is a class which contains a Test in it. How to run a single Test from a Class with Maven … Read more

JUnit 5 Lifecycle methods

In this tutorial we will learn how to use @BeforeEach, @AfterEach @BeforeAll and @AfterAll annotations to execute fixtures in your JUnit 5 Test Classes. @BeforeEach @BeforeEach is used to signal that the annotated method should be executed before each @Test method in the current test class. It is equivalent to JUnit 4’s @Before. Example: package … Read more