首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java 11 junit jupiter assertThrows

Java 11 junit jupiter assertThrows
EN

Stack Overflow用户
提问于 2020-04-07 17:32:04
回答 1查看 328关注 0票数 0

我试图从Java 8迁移到11,但在我的测试类中遇到了一个我不理解的错误。

我失败的(groovy)测试是:

代码语言:javascript
复制
@SpringJUnitConfig
class TestSpringBeanScopeChecker {
    @Autowired
    ApplicationContext ctx

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

SpringBeanScopeChecker:

代码语言:javascript
复制
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());
            }
        }
    }
}

因此,对于测试,我期望得到一个IllegalArgumentException。这在Java8上运行得很好。当我切换到Java11并执行测试时,我得到这个错误:

代码语言:javascript
复制
[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)
EN

回答 1

Stack Overflow用户

发布于 2020-04-17 18:10:26

如果其他人也有同样的问题,我会写下解决方案。问题出在groovy-eclipse-compilergroovy-eclipse-batch的配置错误。

我的groovy版本是由spring-boot管理的,并且我没有根据来自spring-boot pom的groovy.version更新groovy-eclipse-batch。

根据github上的这个问题

您必须在同一版本中使用groovy-eclipse-batchgroovy runtime进行编译。groovy-eclipse-batchgroovy runtime应该匹配。例如,batch 2.5.10-0xruntime 2.5.10batch 3.0.1-0xruntime 3.0.1

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61076889

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档