Saturday 25 May 2019

JUnit5


Release of JUnit 5 on September 10th, 2017

Goals of JUnit 5

1. Add the feature of Java 8
2. Redesign for better integration and extensibility.

Module

• JUnit Platform - The foundation for launching testing frameworks on the JVM. Allows tests to be run from a Console Launcher, or build tools such as Maven and Gradle
 • JUnit Jupiter -  Programming model for writing tests and extensions to JUnit
• JUnit Vintage - Provides a test engine for running JUnit 3 and JUnit 4 tests.


-->

@Test                        Marks a method as a test method 
@ParameterizedTest Marks method as a parameterized test 
@RepeatedTest       Repeat test N times 
@TestFactory       Test Factory method for dynamic tests 
@TestInstance       Used to configure test instance lifecycle 
@TestTemplate       Creates a template to be used by multiple test cases 
@DisplayName       Human friendly name for test 
@BeforeEach       Method to run before each test case 
@AfterEach               Method to run after each test case 
@BeforeAll             Static method to run before all test cases in current class 
@AfterAll                Static method to run after all test cases in current class 
@Nested                  Creates a nested test class 
@Tag                    Declare ‘tags’ for filtering tests 
@Disabled               Disable test or test class 
@ExtendWith     Used to register extensions 



LifeCycle : //todo

public class Greeting {
private static final String HELLO = "Hello";
private static final String WORLD = "World";
public String helloWorld(){
return HELLO + " " + WORLD;
}
public String helloWorld(String name){
return HELLO + " " + name;
}
}
import org.junit.jupiter.api.*;
class GreetingTest {
private Greeting greeting;
@BeforeAll
public static void beforeClass(){
System.out.println("Before - I am only called Once!!!");
}
@BeforeEach
void setUp() {
System.out.println("In Before Each....");
greeting = new Greeting();
}
@Test
void helloWorld() {
System.out.println(greeting.helloWorld());
}
@Test
void helloWorld1() {
System.out.println(greeting.helloWorld("John"));
}
@Test
void helloWorld2() {
System.out.println(greeting.helloWorld("Sam"));
}
@AfterEach
void tearDown() {
System.out.println("In After Each........");
}
@AfterAll
public static void afterClass(){
System.out.println("After!!! ***** - I am only called Once!!!");
}
}