在JUnit 5扩展中获取重复计数的方法是通过实现TestTemplateInvocationContextProvider
接口来创建一个自定义的扩展。以下是一个示例:
TestTemplateInvocationContextProvider
接口的类,例如RepeatCountExtension
。import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;
public class RepeatCountExtension implements TestTemplateInvocationContextProvider {
@Override
public boolean supportsTestTemplate(ExtensionContext context) {
return true;
}
@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
int repeatCount = getRepeatCountFromAnnotation(context);
return Stream.generate(() -> createInvocationContext(repeatCount)).limit(repeatCount);
}
private int getRepeatCountFromAnnotation(ExtensionContext context) {
return context.getRequiredTestMethod().getAnnotation(RepeatCount.class).value();
}
private TestTemplateInvocationContext createInvocationContext(int repeatCount) {
return new TestTemplateInvocationContext() {
@Override
public String getDisplayName(int invocationIndex) {
return "Repetition " + (invocationIndex + 1);
}
@Override
public List<Extension> getAdditionalExtensions() {
return Collections.singletonList(new RepetitionInfoParameterResolver(invocationIndex));
}
};
}
}
@RepeatCount
,用于指定重复计数。import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatCount {
int value() default 1;
}
ParameterResolver
接口的类,例如RepetitionInfoParameterResolver
,用于解析重复计数信息。import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
public class RepetitionInfoParameterResolver implements ParameterResolver {
private final int invocationIndex;
public RepetitionInfoParameterResolver(int invocationIndex) {
this.invocationIndex = invocationIndex;
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return parameterContext.getParameter().getType() == RepetitionInfo.class;
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return new RepetitionInfo(invocationIndex);
}
}
RepetitionInfo
类,用于封装重复计数信息。public class RepetitionInfo {
private final int currentRepetition;
public RepetitionInfo(int currentRepetition) {
this.currentRepetition = currentRepetition;
}
public int getCurrentRepetition() {
return currentRepetition;
}
}
@ExtendWith
注解来注册自定义的扩展,并使用@RepeatCount
注解指定重复计数。import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(RepeatCountExtension.class)
public class MyTest {
@TestTemplate
@RepeatCount(3)
@DisplayName("示例测试")
void test(RepetitionInfo repetitionInfo) {
int repetition = repetitionInfo.getCurrentRepetition();
assertEquals(1, repetition);
// 执行测试逻辑
}
}
在上述示例中,RepeatCountExtension
扩展会根据@RepeatCount
注解中指定的重复计数创建对应数量的TestTemplateInvocationContext
,并通过RepetitionInfoParameterResolver
将重复计数信息注入到测试方法中。在测试方法中,可以使用RepetitionInfo
参数来获取当前重复计数。
请注意,以上示例中的代码仅为演示目的,实际使用时可能需要根据具体需求进行适当调整。
领取专属 10元无门槛券
手把手带您无忧上云