- This module consists of Some Important and Basic Concepts of JUNIT-5 (Unit Testing) which is verymuch supportive for testing our Code with multiple test scripts in Eclipse/Spring framework.
- This JUnit 5 tutorial talks about how it adapted java 8 style of coding and several other features as well.
- JUnit 5 is most widely used testing framework for java applications.
- For very long time, JUnit has been doing its job perfectly.
- In between, JDK 8 brought very exciting features in java and most notably lambda expressions.
- JUnit 5 aims to adapt java 8 style of coding and several other features as well, thatβs why java 8 is required to create and execute tests in JUnit 5 (though it is possible to execute tests written with JUnit 3 or JUnit 4 for backward compatibility).
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
- To be able to launch junit tests, IDEs, build tools or plugins need to include and extend platform APIs. It defines the TestEngine API for developing new testing frameworks that runs on the platform.
- It also provides a Console Launcher to launch the platform from the command line and build plugins for Gradle and Maven.
- It includes new programming and extension models for writing tests.
- It has all new junit annotations and TestEngine implementation to run tests written with these annotations.
- It primary purpose is to support running JUnit 3 and JUnit 4 written tests on the JUnit 5 platform.
- Itβs there are backward compatibility.
You can use JUnit 5 in your maven or gradle project by including minimum two dependencies i.e. Jupiter Engine Dependency and Platform Runner Dependency.
//pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<junit.jupiter.version>5.5.2</junit.jupiter.version>
<junit.platform.version>1.5.2</junit.platform.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
//build.gradle
testRuntime("org.junit.jupiter:junit-jupiter-engine:5.5.2")
testRuntime("org.junit.platform:junit-platform-runner:1.5.2")
@BeforeAll - The annotated method will be run before all test methods in the test class. This method must be static.
@AfterAll - The annotated method will be run after all test methods in the test class. This method must be static.
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import com.gowthamrajk.junit5.examples.Calculator;
public class AppTest {
@BeforeAll
static void setup(){
System.out.println("@BeforeAll executed");
}
@BeforeEach
void setupThis(){
System.out.println("@BeforeEach executed");
}
@Tag("DEV")
@Test
void testCalcOne()
{
System.out.println("======TEST ONE EXECUTED=======");
Assertions.assertEquals( 4 , Calculator.add(2, 2));
}
@Tag("PROD")
@Disabled
@Test
void testCalcTwo()
{
System.out.println("======TEST TWO EXECUTED=======");
Assertions.assertEquals( 6 , Calculator.add(2, 4));
}
@AfterEach
void tearThis(){
System.out.println("@AfterEach executed");
}
@AfterAll
static void tear(){
System.out.println("@AfterAll executed");
}
}
-
Using JUnit 5 test suites, you can run tests spread into multiple test classes and different packages.
-
JUnit 5 provides two annotations: @SelectPackages and @SelectClasses to create test suites.
-
To execute the suite, you will use @RunWith(JUnitPlatform.class).
@RunWith(JUnitPlatform.class) @SelectPackages("com.gowthamrajk.junit5.examples") public class JUnit5TestSuiteExample { }
Additionally, you can use following annotations for filtering test packages, classes or even test methods.
- @IncludePackages and @ExcludePackages to filter packages
- @IncludeClassNamePatterns and @ExcludeClassNamePatterns to filter test classes
- @IncludeTags and @ExcludeTags to filter test methods
@RunWith(JUnitPlatform.class)
@SelectPackages("com.gowthamrajkinjava.junit5.examples")
@IncludePackages("com.gowthamrajk.junit5.examples.packageC")
@ExcludeTags("PROD")
public class JUnit5TestSuiteExample
{
}
-
Assertions help in validating the expected output with actual output of a testcase.
-
To keep things simple, all JUnit Jupiter assertions are static methods in the org.junit.jupiter.Assertions class e.g. assertEquals(), assertNotEquals().
void testCase() { //Test will pass Assertions.assertNotEquals(3, Calculator.add(2, 2)); //Test will fail Assertions.assertNotEquals(4, Calculator.add(2, 2), "Calculator.add(2, 2) test failed"); //Test will fail Supplier<String> messageSupplier = ()-> "Calculator.add(2, 2) test failed"; Assertions.assertNotEquals(4, Calculator.add(2, 2), messageSupplier); }
-
Assumptions class provides static methods to support conditional test execution based on assumptions.
-
A failed assumption results in a test being aborted. Assumptions are typically used whenever it does not make sense to continue execution of a given test method.
-
In test report, these test will be marked as passed.
-
JUnit jupiter Assumptions class has two such methods: assumeFalse(), assumeTrue().
public class AppTest { @Test void testOnDev() { System.setProperty("ENV", "DEV"); Assumptions.assumeTrue("DEV".equals(System.getProperty("ENV")), AppTest::message); } @Test void testOnProd() { System.setProperty("ENV", "PROD"); Assumptions.assumeFalse("DEV".equals(System.getProperty("ENV"))); } private static String message () { return "TEST Execution Failed :: "; } }
- JUnit 4 has been here for a quite long time and there are numerous tests written in junit 4.
- JUnit Jupiter need to support those tests as well. For this purpose, JUnit Vintage sub-project is developed.
- JUnit Vintage provides a TestEngine implementation for running JUnit 3 and JUnit 4 based tests on the JUnit 5 platform.
For more queries, reach me through gowthamraj692@gmail.com or whatsapp @ 9698382306
Code and documentation Copyright 2021 : Gowthamraj K