JUnit

JUnit is a testing framework for Java that allows test cases to be organized into hierarchical suites. The framework uses annotations to declare test cases, to identify methods that setup the environment for tests, and to organize tests cases into suites.

@Test

Any method annotated with @Test is considered to be test case. When running junit you specify a class which contains test cases or a test suite to be run.

Assertions

Inside test methods you can use assertion to check whether a test result matches an expected result. For example, assertEquals(a,b) fails the test case unless a.equals(b) return true.

@Test
public void test() {
   assertEquals(1,1); // test succesful so far
   assertEquals(2,3); // this fails the test
}

Suites

Classes with test cases and packages containing classes with test cases can be grouped into suites. A suite is created by creating an empty class (no fields and methods) and specify the test classes/packages to incorporate into the suite.

@RunWith(JUnitPlatform.class) // run this suite with JUnit
@SelectClasses( { MyFirstTestClass.class, MySecondTestClass.class }) // array of Class objects for test classes to make part of the suite
@SelectPackages( { "edu.iit.mytests" } ) // array of names of package that contain test classes that should be part of the suite
public class MySuite
{
}
In [1]:
%classpath add mvn org.junit.jupiter junit-jupiter-api 5.3.2
%classpath add mvn org.junit.jupiter junit-jupiter-engine 5.3.2
%classpath add mvn org.junit.vintage junit-vintage-engine 5.3.2
%classpath add mvn org.junit.platform junit-platform-launcher 1.3.2
%classpath add mvn org.junit.platform junit-platform-runner 1.3.2   
%classpath add mvn org.junit.platform junit-platform-console 1.3.2
    
package lecture;

import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

public class TestCase {
    
    // method that will be executed once before any test method is run 
    @BeforeAll
    public static void globalSetup() {
        System.out.println("before all test cases");
    }

    // method that will be executed once before any test method is run 
    @AfterAll
    public static void globalTearDown() {
        System.out.println("after all test cases");
    }

    // method that will be executed before each testcase
    @BeforeEach
    public void setup() {
        System.out.println("before a test");
    }
    
    // method that will be exected after each testcase
    @AfterEach
    public void tearDown() {
        System.out.println("after a test");
    }
    
    // a test case
    @Test
    public void test1 () {
        assertEquals(1,1);
    }
    
    // another test case
    @Test
    public void test2() {
        assertEquals(1,2);
    }
    
    
}
Wrong command format, should be%classpath add mvn group name version [type classifier] or %classpath add mvn group:name:version[:type:classifier]
Out[1]:
lecture.TestCase
In [17]:
import org.junit.jupiter.api.*;
import org.junit.platform.launcher.listeners.*;
import org.junit.platform.launcher.*;
import org.junit.platform.launcher.core.*;
import static org.junit.platform.engine.discovery.DiscoverySelectors.*;
import lecture.TestCase;

SummaryGeneratingListener listener = new SummaryGeneratingListener();
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
          .selectors(selectClass(TestCase.class))
          .build();
Launcher launcher = LauncherFactory.create();
TestPlan testPlan = launcher.discover(request);
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
ERROR: org.junit.platform.commons.util.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath