Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to repeat a parameterized Junit 5 test?

Tags:

java

junit

junit5

Is there a way to use @RepeatedTest annotation alongside with @TestTemplate?

The objective is to run test multiple times for each type of Dependency, which is injected by an Extension class.

    @TestTemplate
    @RepeatedTest(100)
    @Timeout(1)
    void test(final Dependency dep) throws Exception {
        .... 
    }

like image 421
andreoss Avatar asked Oct 19 '25 06:10

andreoss


1 Answers

Note: Example below provides implementation of custom @TestTemplate using custom Dependency class implementation

Consider this:

import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class RepeatedParametrizedTest {

  @TestTemplate
  @ExtendWith(MyTemplate.class)
  @Timeout(1)
  void test(final Dependency dep) {
    assertEquals(true, true);
  }
}

class Dependency {

  private final String name;

  public Dependency(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }

  @Override
  public String toString() {
    return name;
  }

}

class MyTemplate implements TestTemplateInvocationContextProvider {

  @Override
  public boolean supportsTestTemplate(ExtensionContext context) {
    return true;
  }

  @Override
  public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
    // here goes your implementation of all possible Dependency objects wrapped in invocationContext()
    return IntStream.range(0, 100)
        .flatMap(n -> IntStream.range(1, 10))
        .mapToObj(n -> invocationContext(new Dependency("dependency" + n)));
  }

  private TestTemplateInvocationContext invocationContext(Dependency dependency) {
    return new TestTemplateInvocationContext() {
      @Override
      public String getDisplayName(int invocationIndex) {
        return dependency.getName();
      }

      @Override
      public List<Extension> getAdditionalExtensions() {
        return Collections.singletonList(new ParameterResolver() {
          @Override
          public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
            return parameterContext.getParameter().getType().equals(Dependency.class);
          }

          @Override
          public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
            return dependency;
          }
        });
      }
    };
  }
}

For 10 * 10 instances that would produce:

IntelliJ IDEA test output

like image 192
gokareless Avatar answered Oct 20 '25 19:10

gokareless



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!