我试图从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。这在Java8上运行得很好。当我切换到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)发布于 2020-04-17 18:10:26
如果其他人也有同样的问题,我会写下解决方案。问题出在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。
https://stackoverflow.com/questions/61076889
复制相似问题