简体   繁体   English

Java 11 junit 木星断言抛出

[英]Java 11 junit jupiter assertThrows

I try to migrate from Java 8 to 11 and get an error in my test class that I don't understand.我尝试从 Java 8 迁移到 11 并在我的测试 class 中遇到我不明白的错误。

My failing (groovy) test is:我失败的(常规)测试是:

@SpringJUnitConfig
class TestSpringBeanScopeChecker {
    @Autowired
    ApplicationContext ctx

    @Test
    void testSingletonFail() {
        Assertions.assertThrows(IllegalStateException.class) {
            SpringBeanScopeChecker.check(ctx, DummyPrototype.class, BeanDefinition.SCOPE_SINGLETON)
        }
    }
}

The SpringBeanScopeChecker: SpringBeanScopeChecker:

public class SpringBeanScopeChecker {

    private SpringBeanScopeChecker() {}

    public static void check(ApplicationContext ctx, Class<?> type, String scope)
            throws IllegalStateException {

        AbstractApplicationContext actx = (ctx instanceof AbstractApplicationContext) ? 
                ((AbstractApplicationContext) ctx) : 
                new StaticApplicationContext(ctx);

        ConfigurableListableBeanFactory factory = actx.getBeanFactory();

        for (String key : ctx.getBeanNamesForType(type)) {
            BeanDefinition definition = factory.getMergedBeanDefinition(key);

            if (!scope.equals(definition.getScope())) {
                throw new IllegalStateException(
                        "Every spring bean "
                                + "must be request scoped in the bean configuration. The current scope is: "
                                + definition.getScope());
            }
        }
    }
}

So for the test I'm expecting a IllegalArgumentException .因此,对于测试,我期待IllegalArgumentException And this is working fine with Java8 .这适用于Java8 When I switch to Java11 and execute the test I get this error:当我切换到Java11并执行测试时,我收到此错误:

[ERROR] testSingletonFail  Time elapsed: 0.009 s  <<< FAILURE!
org.opentest4j.AssertionFailedError: Unexpected exception type thrown
==> expected: <java.lang.IllegalStateException> but was: <java.lang.AbstractMethodError>
         at TestSpringBeanScopeChecker.testSingletonFail(TestSpringBeanScopeChecker.groovy:22)
 Caused by: java.lang.AbstractMethodError: Receiver class
 TestSpringBeanScopeChecker does not define or inherit an
 implementation of the resolved method 'abstract java.lang.Object
 getProperty(java.lang.String)' of interface groovy.lang.GroovyObject.
         at TestSpringBeanScopeChecker.testSingletonFail(TestSpringBeanScopeChecker.groovy:22)

In case someone else has the same problem I write down the solution for this.如果其他人有同样的问题,我会为此写下解决方案。 The problem was misconfiguration of the groovy-eclipse-compiler and groovy-eclipse-batch .问题是groovy-eclipse-compilergroovy-eclipse-batch配置错误。

My groovy version is managed by spring-boot and I didn't update the groovy-eclipse-batch according to the groovy.version from the spring-boot pom. My groovy version is managed by spring-boot and I didn't update the groovy-eclipse-batch according to the groovy.version from the spring-boot pom.

According to this issue on github :根据github上的这个问题:

You have to compile with groovy-eclipse-batch and groovy runtime in the same version.您必须在同一版本中使用groovy-eclipse-batchgroovy runtime进行编译。 groovy-eclipse-batch and groovy runtime should be matched up. groovy-eclipse-batchgroovy runtime应该匹配。 Eg batch 2.5.10-0x and runtime 2.5.10 or batch 3.0.1-0x and runtime 3.0.1 .例如batch 2.5.10-0xruntime 2.5.10batch 3.0.1-0xruntime 3.0.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JUNIT5 中的 AssertThrows - AssertThrows in JUNIT5 JUnit 5 assertThrows() 忽略抛出的异常 - JUnit 5 assertThrows() ignores thrown exception JUnit assertThrows中的Executable会发生什么? - What happens with the Executable in a JUnit assertThrows? Junit:期望/ assertThrows 不起作用 - Junit: expect / assertThrows doesn't work java.lang.NoClassDefFoundError: org/junit/jupiter/api/extension/ScriptEvaluationException - java.lang.NoClassDefFoundError: org/junit/jupiter/api/extension/ScriptEvaluationException IntelliJ + JUnit 5(木星) - IntelliJ + JUnit 5 (Jupiter) JUnit 5 migration - how to solve java.lang.ClassNotFoundException: org.junit.jupiter.api.extension.ReflectiveInvocationContext - JUnit 5 migration - how to solve java.lang.ClassNotFoundException: org.junit.jupiter.api.extension.ReflectiveInvocationContext Eclipse Java IDE JUnit5:junit.jupiter.api.Assertions 不可访问 - Eclipse Java IDE JUnit5: junit.jupiter.api.Assertions is not accessible pom.xml Maven配置在混合Java + Kotlin项目中运行JUnit 4 + JUnit 5 Jupiter测试 - pom.xml Maven configuration to run JUnit 4 + JUnit 5 Jupiter tests in mixed Java + Kotlin project JUnit java.lang.NoSuchMethodError: org.junit.jupiter.api.extension.ExtensionContext.getRequiredTestInstances() - JUnit java.lang.NoSuchMethodError: org.junit.jupiter.api.extension.ExtensionContext.getRequiredTestInstances()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM