Author

Author- Ram Ranjeet Kumar

Sunday, July 30, 2023

Junit5 Annotations


 

JUnit5 annotations

  • @Test: This annotation is similar to the one in JUnit 4 and is used to mark a method as a test method.
@Test
public void testAddition() {
    // Test logic and assertions go here
}


  • @BeforeEach: This annotation is used to mark a method that should be executed before each test method in the class. It is typically used to set up the test environment or initialize resources.
@BeforeEach
public void setUp() {
    // Initialization logic goes here
}
  • @AfterEach: This annotation is used to mark a method that should be executed after each test method in the class. It is typically used to clean up resources or perform cleanup operations.
@AfterEach
public void tearDown() {
    // Cleanup logic goes here
}

  • @BeforeAll: This annotation is used to mark a method that should be executed once before any test methods in the class. It is used for setup that should be performed only once.
@BeforeAll
public static void setUpClass() {
    // One-time setup logic goes here
}

  • @AfterAll: This annotation is used to mark a method that should be executed once after all test methods in the class have been executed. It is used for cleanup that should be performed only once.
@AfterAll
public static void tearDownClass() {
    // One-time cleanup logic goes here
}

  • @RepeatedTest : This annotation is used in JUnit 5 to repeat a test multiple times. The simplest way of using the annotation is to pass an integer value as an argument. Here is an example of how to use the annotation:
@RepeatedTest(5)
public void testRepeatedTest() {
    System.out.println("Hello World");
}
This will run the annotated method 5 times.

@ParameterizedTest:  This annotation is used in JUnit 5 to run the same test multiple times with different arguments. Here is an example of how to use the annotation:

@ParameterizedTest
@ValueSource(strings = { "madam", "java", "racecar" })
void palindromes(String value) {
    assertTrue(StringUtils.isPalindrome(value));
}

This will run the test method three times with the arguments "madam", "java, and "racecar" respectively.

  • @TestFactory: This  annotation is used in JUnit 5 to create dynamic tests. A dynamic test is a test that is generated at runtime by a factory method using the @TestFactory annotation. The method marked @TestFactory is not a test case, rather it’s a factory for test cases. Here is an example of how to use the annotation:
@TestFactory
Stream<DynamicTest> dynamicTestsFromStream() {
    return Stream.of("racecar", "radar", "able was I ere I saw elba")
      .map(text -> DynamicTest.dynamicTest("Palindrome test for " + text, () -> assertTrue(StringUtils.isPalindrome(text))));
}

This will create three dynamic tests with the arguments "racecar", "radar", and "able was I ere I saw elba" respectively.


  • @Disabled: This annotation is used to disable a test method or an entire test class during test execution. It is similar to @Ignore in JUnit 4.
@Disabled
@Test
public void disabledTestMethod() {
    // This test method will be disabled during test execution
}
  • @DisplayName: This annotation is used to provide a custom display name for a test method or a test class. It allows you to give more descriptive names to your tests.
@Test
@DisplayName("Test addition operation")
public void testAddition() {
    // Test logic and assertions go here
}
  • @Nested: This annotation is used to define nested test classes. Nested test classes can access the private fields and methods of the outer class, allowing better test organization.
public class OuterTestClass {

    @Nested
    class InnerTestClass {
        // Test methods and other annotations go here
    }
}

  • @TestClassOrder:  It is used in JUnit 5 to configure a ClassOrderer for the @Nested test classes of the annotated test class. If @TestClassOrder is not explicitly declared on a test class, inherited from a parent class, declared on a test interface implemented by a test class, or inherited from an enclosing class, @Nested test classes will be executed in arbitrary order. As an alternative to @TestClassOrder, a global ClassOrderer can be configured for the entire test suite via the "junit.jupiter.testclass.order.default" configuration parameter¹. 

Here's an example of how to use @TestClassOrder:


@TestClassOrder(ClassOrderer.OrderAnnotation.class)
class OrderedNestedTests {
    @Nested
    @Order(1)
    class PrimaryTests {
        // @Test methods ...
    }

    @Nested
    @Order(2)
    class SecondaryTests {
        // @Test methods ...
    }
}

This will guarantee that @Nested test classes are executed in the order specified via the @Order annotation.


  • @TestMethodOrder:  It is used in JUnit 5 to control the execution order of tests. We can use our own MethodOrderer, as we'll see later. Or we can select one of three built-in orderers:
    • Alphanumeric Order, 
    • @Order Annotation, 
    • Random Order. 

Here's an example of how to use @TestMethodOrder

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class OrderedTests {
    @Test
    @Order(1)
    void nullValues() {}

    @Test
    @Order(2)
    void emptyValues() {}

    @Test
    @Order(3)
    void validValues() {}
}

This will guarantee that test methods are executed in the order specified via the @Order annotation




These are some of the essential annotations provided by JUnit 5. JUnit 5 also introduces many other features and improvements, such as parameterized tests, dynamic tests, test interfaces, and more, making it a powerful and modern testing framework for Java developers.

No comments:

Post a Comment