有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在元注释中有一个自定义TestContextBootstrapper

我有一个元注释,用于注释我的测试类:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@SpringBootTest
@BootstrapWith(MyOwnTestContextBootstrapper.class)
public @interface IntegrationTest {
.....

我的测试类看起来像:

@IntegrationTest
class CreateSubscriptionServiceTest {
    @Autowired
    .......

然后我得到了一个例外: found multiple declarations of @BootstrapWith 详情:

java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.my.project.CreateSubscriptionServiceTest]: [@org.springframework.test.context.BootstrapWith(value=com.my.project.MyOwnTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=org.springframework.boot.test.context.SpringBootTestContextBootstrapper)]

    at org.springframework.test.context.BootstrapUtils.resolveExplicitTestContextBootstrapper(BootstrapUtils.java:176)
    at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:130)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:122)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore.lambda$getOrComputeIfAbsent$4(ExtensionValuesStore.java:86)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore$MemoizingSupplier.computeValue(ExtensionValuesStore.java:223)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore$MemoizingSupplier.get(ExtensionValuesStore.java:211)
    at org.junit.jupiter.engine.execution.ExtensionValuesStore$StoredValue.evaluate(ExtensionValuesStore.java:191)
....

共 (1) 个答案

  1. # 1 楼答案

    我找到了答案

    @SpringBooTest替换为@ExtendWith(SpringExtension.class),使元注释看起来像:

    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @ExtendWith(SpringExtension.class)
    @BootstrapWith(MyOwnTestContextBootstrapper.class)
    public @interface IntegrationTest {
    .....