Java 11中的junit jupiter assertThrows

huangapple 未分类评论47阅读模式
英文:

Java 11 junit jupiter assertThrows

问题

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

我的失败(Groovy)测试如下:

@SpringJUnitConfig
class TestSpringBeanScopeChecker {
    @Autowired
    ApplicationContext ctx

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

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());
            }
        }
    }
}

所以,对于这个测试,我期望得到IllegalArgumentException异常。这在Java 8下正常工作。
但是当我切换到Java 11并执行测试时,我得到以下错误:

[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)
英文:

I try to migrate from Java 8 to 11 and get an error in my test class that I don't understand.

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:

public class SpringBeanScopeChecker {

	private SpringBeanScopeChecker() {}

	public static void check(ApplicationContext ctx, Class&lt;?&gt; 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(
						&quot;Every spring bean &quot;
								+ &quot;must be request scoped in the bean configuration. The current scope is: &quot;
								+ definition.getScope());
			}
		}
	}
}

So for the test I'm expecting a IllegalArgumentException. And this is working fine with Java8.
When I switch to Java11 and execute the test I get this error:

[ERROR] testSingletonFail  Time elapsed: 0.009 s  &lt;&lt;&lt; FAILURE!
org.opentest4j.AssertionFailedError: Unexpected exception type thrown
==&gt; expected: &lt;java.lang.IllegalStateException&gt; but was: &lt;java.lang.AbstractMethodError&gt;
         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 &#39;abstract java.lang.Object
 getProperty(java.lang.String)&#39; of interface groovy.lang.GroovyObject.
         at TestSpringBeanScopeChecker.testSingletonFail(TestSpringBeanScopeChecker.groovy:22)

答案1

得分: 0

为了防止其他人遇到相同的问题,我将解决方案写在下面。
问题出在 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.10,或者 batch 3.0.1-0xruntime 3.0.1

英文:

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.

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:

You have to compile with groovy-eclipse-batch and groovy runtime in the same version. groovy-eclipse-batch and groovy runtime should be matched up. E.g. batch 2.5.10-0x and runtime 2.5.10 or batch 3.0.1-0x and runtime 3.0.1.

huangapple
  • 本文由 发表于 2020年4月7日 17:32:04
  • 转载请务必保留本文链接:https://java.coder-hub.com/61076889.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定