Getting started with JUnit

  JUnit is the most popular unit Testing framework in Java. It is explicitly recommended for Unit Testing. JUnit does not require server for testing web application, which makes the testing process fast. JUnit framework also allows quick and easy generation of test cases and test data. The org.Junit package consist of many interfaces and … Read more

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 IntelliJ Project, then we will show how to create a JUnit 5 project with Maven. Creating a JUnit IntelliJ Project: Requirements: Download IntelliJ Community edition from: Install IntelliJ Idea, then launch … 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 run repeated tests in JUnit

The @RepeatedTest annotation enable to run multiple times single tests in JUnit 5. The number of times the test will be executed can be configured as parameter to @RepeatedTest annotation. Example: @Test @RepeatedTest(5) void addNumber() { Calculator calculator = new Calculator(); Random rand = new Random(); int n = rand.nextInt(50); Assertions.assertEquals(n*2, calculator.add(n, n), “Checking calculator … Read more

How to assert Exceptions in JUnit

In JUnit 5 you can use Assertions.assertThrows to assert that a particular Exception is thrown in your test. Example: A very simple example can be: @Test void testExpectedException() { Assertions.assertThrows(NumberFormatException.class, () -> { Integer.parseInt(“abc”); }); } If you are using Junit 4.7 you can use the ExpectedException Rule: public class SampleTest { @Rule public final … 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

Getting started with JUnit 5 TestSuite

A JUnit 5 TestSuite can be used to to run multiple tests together. They allow to aggregate multiple test classes. JUnit 5 provides the following annotations: @SelectPackages – used to specify the names of packages for the test suite @SelectClasses – used to specify the classes for the test suite. They can be located in … Read more