【问题标题】:How to repeat a parameterized Junit 5 test?如何重复参数化的 Junit 5 测试?
【发布时间】:2021-02-10 14:45:02
【问题描述】:

有没有办法将@RepeatedTest 注释与@TestTemplate 一起使用?

目标是对Dependency 的每种类型运行多次测试,由Extension 类注入。

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

【问题讨论】:

    标签: java junit junit5


    【解决方案1】:

    注意:下面的示例提供了使用自定义 Dependency 类实现的自定义 @TestTemplate 实现

    考虑一下:

    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;
              }
            });
          }
        };
      }
    }
    

    对于 10 * 10 个实例,会产生:

    【讨论】:

    • 您的案例将需要现有的测试模板修改/复制并与上面的代码合并
    猜你喜欢
    • 2022-10-06
    • 1970-01-01
    • 2018-10-01
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多