JUnit 5

JUnit is the test framework of Java: it finds the methods marked as tests, runs them, and prints a report that says what passed and what did not. The pages here use JUnit 5, the current generation of the framework, with the standalone console launcher: two commands build and run a test class, with no build tool and no network.

JUnit 5 course: from the first test to a full suite The launcher and its report, assertions, the lifecycle, parameterised tests, exceptions, files, tags, suites and a capstone. Open Testing Java code with JUnit 5 The console launcher, assertions and failures, parameterised tests, the lifecycle, exceptions, temporary folders, the project layout and the red-green loop. Open

All articles

The original page

The page that was at /junit/ before this hub keeps its two examples here, as they were written: an editor generated the test class, and the project connected JUnit through the IDE. The course above replaces that setup with the launcher and the two commands that this machine can run.

Simple test

A class with one method to test:

package ru.andreyolegovich.it.junit; public class Main { public static void main(String[] args) { } static int addFunc(int var_1, int var_2){ int sum = var_1 + var_2; return sum; } }

In IntelliJ, right-click on main → Go To → Test → Create New Test → Select JUnit 5 → Fix. The generated test class looks like this:

package com.pluralsight.organized; import org.junit.jupiter.api.Test; import static com.pluralsight.organized.Main.addFunc; import static org.junit.jupiter.api.Assertions.*; class MainTest { @Test public void addCorrectly() { assertEquals(4,addFunc(1,3)); } }