Table of contents
1.
Introduction
2.
Basic Syntax
3.
Lifecycle of Dynamic Tests
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Dynamic Tests in Junit 5

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JUnit is a framework in Java used for unit testing. It is vital in the development of test-driven development. It is part of the xUnit frameworks. We link the JUnit as a JAR file at compile time. JUnit5 resides under the org.junit.jupiter package in Java.

import org.junit.jupiter.api.*;
You can also try this code with Online Java Compiler
Run Code

A Dynamic Test is one of the most commonly used tests, and it is generated at the run time. We use the TestFactory method to generate Dynamic Tests in JUnit5. TestFactory method is a non-static and non-private method having a return type as Stream, Collection, Iterator, Iterable, or an array of Dynamic Tests. Providing any other return type to TestFactory will result in an error. 

Basic Syntax

Let us look at an example of Dynamic Tests in this section to understand the Syntax,

@TestFactory
Stream<DynamicTest> dynamicTestStream() {
  return IntStream.of(0, 2, 4, 6)
    .mapToObj(v ->
      dynamicTest(v + " is a multiple of 2",()->assertEquals(0,v%2))
    );
}
You can also try this code with Online Java Compiler
Run Code

In the above code, first, we invoke the TestFactory method so that the compiler can recognize it as a test factory containing multiple Dynamic Tests.

We have selected the method's return type as a stream of Dynamic Tests. We then test whether the stream of numbers is divisible by two or not.

As discussed earlier, many more options are available for choosing the return type.

@TestFactory
//Dynamic Test with return type as Iterable
Iterable<DynamicTest> dynamicTestsWithIterable() {
    return Arrays.asList(
      DynamicTest.dynamicTest("Add test",
        () -> assertEquals(2, Math.addExact(1, 1))),
      DynamicTest.dynamicTest("Multiply Test",
        () -> assertEquals(4, Math.multiplyExact(2, 2))));
}


@TestFactory
//Dynamic Test with return type as Iterator
Iterator<DynamicTest> dynamicTestsWithIterator() {
    return Arrays.asList(
      DynamicTest.dynamicTest("Add test",
        () -> assertEquals(2, Math.addExact(1, 1))),
      DynamicTest.dynamicTest("Multiply Test",
        () -> assertEquals(4, Math.multiplyExact(2, 2))))
        .iterator();
}


@TestFactory
//Dynamic test with return type as collection of Dynamic Test
Collection<DynamicTest> dynamicTestsWithCollection() {
    return Arrays.asList(
      DynamicTest.dynamicTest("Add test",
        () -> assertEquals(2, Math.addExact(1, 1))),
      DynamicTest.dynamicTest("Multiply Test",
        () -> assertEquals(4, Math.multiplyExact(2, 2))));
}
You can also try this code with Online Java Compiler
Run Code

Check out JUnit Interview Questions here.

Lifecycle of Dynamic Tests

The execution cycle of the Dynamic Test is a little different from that of the standard tests. Let us compare both of them in this section.

 

Standard Test Method

 

Test Factory

 

Execute BeforeEach

 

Execute BeforeEach

 

Execute Test

 

Execute TestFactory

  • Execute Dynamic Test 1
  • Execute Dynamic Test 2
  • Execute Dynamic Test 3
  • ….

 

Execute AfterEach

 

 

Execute AfterEach

There are two very significant reasons why one should opt for Dynamic Testing using TestFactory instead of regular tests,

  • Dynamic Testing provides the option of expressing the tests at runtime instead of expressing them at compile-time like other tests. We can easily perform a Dynamic Test by using the following code snippet,
DynamicTest.dynamicTest(String, URI, Executable)
DynamicContainer.dynamicContainer(String, URI, Stream)
  • One other essential reason to opt for Dynamic Testing is the presence of multiple return types. The Parameterized tests provide only the basic Java return types such as String, Int, Double. Thus we cannot pass other tests or exceptions as input is impossible.

FAQs

  1. What is JUnit?
    JUnit is a framework in Java used for unit testing. It is vital in the development of test-driven development. It is part of the xUnit frameworks. We link the JUnit as a JAR file at compile time. JUnit5 resides under the org.junit.jupiter package in Java.
     
  2. What is the latest version of JUnit?
    The most recent version of JUnit is Junit5, although the previous versions of Junit are still equally famous. For instance, JUnit4 has over 100,000 usages.
     
  3. What is Dynamic Testing?
    A Dynamic Test is one of the most commonly used tests, and it is generated at the run time. We use the TestFactory method to generate Dynamic Tests in JUnit5. TestFactory method is a non-static and non-private method.
     
  4. What are the return types in the Test Factory Method?
    Test Factory provides the option of multiple return types such as Stream, Collection, Iterator, Iterable, or an array of Dynamic Tests. Providing any other return type to TestFactory will result in an error. 
     
  5. What is the advantage of using Dynamic Testing?
    One essential reason to opt for Dynamic Testing is the presence of multiple return types. The Parameterized tests provide only the basic Java return types such as String, Int, Double.

Key Takeaways

This Blog covered all the necessary points about the JUnit Platform Test Kit and all the features of the JUnit Platform Test Kit. We further discussed how to implement the JUnit Platform Test Kit in Java.

Don’t stop here; check out Coding Ninjas for more unique courses and guided paths. Also, try Coding Ninjas Studio for more exciting articles, interview experiences, and fantastic Data Structures and Algorithms problems.

Live masterclass