[英]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-compiler
和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. 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-batch
和groovy runtime
进行编译。 groovy-eclipse-batch
and groovy runtime
should be matched up. groovy-eclipse-batch
和groovy 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-0x
和runtime 2.5.10
或batch 3.0.1-0x
和runtime 3.0.1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.