Site icon TestingDocs.com

Explain some Jupiter Junit5 Annotations?

There are many new annotations supported by Junit 5. We can find the annotations under the Jupiter package

org.junit.jupiter.api

JUnit 5 Annotations

Some of the annotationsin JUnit 5 are as follows:

•@BeforeAll
•@AfterAll
•@BeforeEach
•@AfterEach
•@Disabled

Sample Listing with the lifecycle annotations in the JUnit 5

Java Listing

package test;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

@TestInstance(Lifecycle.PER_CLASS)
class JUnit5LifeCycle {

    @BeforeAll
    void setUpOnce() {
        System.out.println("@BeforeAll annotated method");
    }

    @AfterAll
    void tearDownOnce() {
        System.out.println("@AfterAll annotated method");
    }

    @AfterEach
    void tearDownEachMethodOnce() {
        System.out.println("@AfterEach annotated method");
    }

    @BeforeEach
    void setupEachMethodOnce() {
        System.out.println("@BeforeEach annotated method");
    }

    @Test
    void testCodeMethod() {
        System.out.println("@Test annotated method");
    }

    @Disabled
    void disabledTest() {
        System.out.println("@Disabled test annotated method");
    }
}

Output:

@BeforeAll annotated method
@BeforeEach annotated method
@Test annotated method
@AfterEach annotated method

Screenshot

Exit mobile version