【问题标题】:AbstractMethodError For @BeforeAll when using SpringExtension.beforeAll使用 SpringExtension.beforeAll 时 @BeforeAll 的 AbstractMethodError
【发布时间】:2018-09-06 07:42:43
【问题描述】:

当尝试使用 Junit 运行我的 Spring Boot 应用程序集成测试时,我收到以下错误,与 @BeforeAll 标记和 @AfterAll 标记不允许使用有关。

我正在学习本教程,但由于 Intellij 没有正确选择 Jupiter runner,因此需要向我的 POM 添加一些内容: https://info.michael-simons.eu/2018/06/18/maven-use-junit-5-with-spring-boot-for-unit-and-integration-tests/

java.lang.AbstractMethodError: org.springframework.test.context.junit.jupiter.SpringExtension.beforeAll(Lorg/junit/jupiter/api/extension/ContainerExtensionContext;)V

at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$invokeBeforeAllCallbacks$3(ClassTestDescriptor.java:197)
at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.invokeBeforeAllCallbacks(ClassTestDescriptor.java:197)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.before(ClassTestDescriptor.java:152)
at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.before(ClassTestDescriptor.java:61)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$1(HierarchicalTestExecutor.java:79)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:76)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$1(HierarchicalTestExecutor.java:91)
at org.junit.platform.engine.support.hierarchical.SingleTestExecutor.executeSafely(SingleTestExecutor.java:66)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:76)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:51)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:43)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:137)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:87)
at org.junit.platform.launcher.Launcher.execute(Launcher.java:93)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:59)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Suppressed: java.lang.AbstractMethodError: org.springframework.test.context.junit.jupiter.SpringExtension.afterAll(Lorg/junit/jupiter/api/extension/ContainerExtensionContext;)V
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$null$7(ClassTestDescriptor.java:232)
    at org.junit.jupiter.engine.execution.ThrowableCollector.execute(ThrowableCollector.java:40)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.lambda$invokeAfterAllCallbacks$8(ClassTestDescriptor.java:232)
    at java.util.ArrayList.forEach(ArrayList.java:1249)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.invokeAfterAllCallbacks(ClassTestDescriptor.java:232)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.after(ClassTestDescriptor.java:168)
    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.after(ClassTestDescriptor.java:61)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.lambda$execute$1(HierarchicalTestExecutor.java:96)
    ... 19 more

我的 Pom 依赖项是:

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.0.0-M4</version>
        <scope>test</scope>
    </dependency>
    <!-- and the engine for surefire and failsafe -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.0.0-M4</version>
        <scope>test</scope>
    </dependency>

    <!-- Explicitly required for Intellij -->
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>1.0.0-M4</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-commons</artifactId>
        <version>1.0.0-M4</version>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.21.0</version>
        <scope>test</scope>
    </dependency>

</dependencies>

还有我的集成套件:

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class GameIntegrationTests {

    @Test
    public void test() {

    }
}

【问题讨论】:

    标签: java spring-boot integration-testing junit5 spring-boot-test


    【解决方案1】:

    您正在使用该版本的 Spring Boot 不支持的 JUnit 5 M4(即里程碑版本 4)。

    您应该将 JUnit Platform 1.1.1 和 JUnit Jupiter 5.1.1 或更高版本与 Spring Boot 2.0.3 一起使用。

    确保您还使用JUnit 5.1.1 User Guide 中记录的 Maven Surefire 插件的版本 2.19.1

    【讨论】:

    • 使用 1.1.1 和 5.1.1 版本确实解决了我的问题,但我发现我仍然需要为 Maven Surefire 19.1.0 使用较低版本,因为 maven 没有在 2.22 版本上进行测试.0.我还需要从 16.0.3 更新我的 Intellij 版本,以便能够运行我的任何 junit 5 测试。
    • 很高兴你把它整理出来!而且...抱歉推荐了错误的 Surefire 版本。
    • 仅供参考:我更新了答案以指向 5.1.1 的用户指南关于受支持版本的 Surefire,它应该是 2.19.1(不是你提到的 19.1.0 - 对?)。
    • 是的,我正在使用 2.19.1,不知道为什么我说 19.1.0。道歉
    猜你喜欢
    • 1970-01-01
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2019-06-28
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多