JUnit: How to run a test n iterations

On some occasions it can be we need to execute a test n times in order to ensure its proper operation.

We have many formulas for this. First, let’s consider the following scenario:

  • We want to test a method that generates a random number of n digits. The value of n is passed as a parameter to that method.
  • We create a JUnit test class in which we declare the number of repetitions or iterations that we want a certain test to execute.
private static final int NUMBER_REPETITIONS = 1_000_000;
enter fullscreen mode

exit fullscreen mode

  • We create tests to validate the functionality of the method. In this example, I have created a test using JUnit annotations @RepeatedTest And another simple test where we do all the logic inside a do-while loop.

Test A:using annotations @RepeatedTest,

import org.junit.jupiter.api.RepeatedTest;

@RepeatedTest(value = NUMBER_REPETITIONS)
void random_number_with_four_digits() {

    var low = 1000;
    var high = 9999;
    var numOfDigits = 4;
    var actual = RandomUtil.generateNumber(numOfDigits);
    assertTrue(high >= actual, "Error, random is too high");
    assertTrue(low <= actual, "Error, actual is too low");
}
enter fullscreen mode

exit fullscreen mode

Test B: Using a do-while loop.

@Test
void random_numbers_generator() {

    int iterationCount = 1;
    do {
        var low = 1000;
        var high = 9999;
        var numOfDigits = 4;
        var actual = RandomUtil.generateNumber(numOfDigits);
        assertTrue(high >= actual, "Error, random is too high");
        assertTrue(low <= actual, "Error, actual is too low");
        iterationCount++;
    } while (iterationCount <= NUMBER_REPETITIONS);
}
enter fullscreen mode

exit fullscreen mode

as we can see both tests say the same thingthat the random number generator method is going to return a value between low and high.
In both the cases the test will be executed multiple times as we indicate in the constant NUMBER_REPETITIONS,

Once we have this scenario, We can do various tests changing the value of low, high, numOfDigits You NUMBER_REPETITIONS,

this time i wanted to calculate difference in execution time Each type of test, and these are the tests I’ve done:

number of iterations Test A (@RepeatedTest) Test B (Do-Time)
1_000 130 ms 13 ms
100_000 2600 ms 20 ms
1_000_000 unexpected 55 ms

In this example, the test executed Using a do-while loop has been about 90% faster than executed by annotating @RepeatedTestApart from the fact that in case of 1_000_000 iterations, I am not able to get the final result since the application hangs.

Leave a Comment