英文:
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<?> 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
. 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 <<< 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)
答案1
得分: 0
为了防止其他人遇到相同的问题,我将解决方案写在下面。
问题出在 groovy-eclipse-compiler
和 groovy-eclipse-batch
的配置错误上。
我的 Groovy 版本由 Spring Boot 管理,我没有根据 Spring Boot 的 pom 文件中的 groovy.version
更新 groovy-eclipse-batch
。
根据这个在 github 上的问题:
你必须使用相同版本的 groovy-eclipse-batch
和 groovy runtime
进行编译。groovy-eclipse-batch
和 groovy runtime
需要匹配。例如,batch 2.5.10-0x
和 runtime 2.5.10
,或者 batch 3.0.1-0x
和 runtime 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
.
专注分享java语言的经验与见解,让所有开发者获益!
评论