升级到 Java 8 会导致 unittest java.io.IOException 中的 Orika 映射器:无效常量类型:15 at 142

Upgrade to Java 8 causes Orika mapper in unittest java.io.IOException: invalid constant type: 15 at 142

我有一个项目,我想将版本从 Java 1.7 更新到 Java 1.8,但是 运行 UnitTests 所有映射器测试都失败了。

该项目正在使用: SpringJUnit4ClassRunner

<dependency>
    <groupId>ma.glasnost.orika</groupId>
    <artifactId>orika-core</artifactId>
    <version>1.4.5</version>
</dependency>

对于那部分例外:nested exception is ma.glasnost.orika.MappingException: java.lang.RuntimeException: java.io.IOException: invalid constant type: 15 at 142 我发现 u.a 这个 SO post:
但我不确定这是否真的是问题所在,因为我不依赖 'javassist'.

这是否与同一问题有关? 我该如何克服它?

更完整的 StackTrace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.company.APP.common.mapping.orika.configuration.mappers.user.UserMapperTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected ma.glasnost.orika.MapperFacade com.company.APP.common.mapping.orika.configuration.mappers.BaseMapperTest.mapper; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mapperFacadeFactoryBean': FactoryBean threw exception on object creation; nested exception is ma.glasnost.orika.MappingException: java.lang.RuntimeException: java.io.IOException: invalid constant type: 15 at 142

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:82)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:199)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:251)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:253)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected ma.glasnost.orika.MapperFacade com.company.APP.common.mapping.orika.configuration.mappers.BaseMapperTest.mapper; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mapperFacadeFactoryBean': FactoryBean threw exception on object creation; nested exception is ma.glasnost.orika.MappingException: java.lang.RuntimeException: java.io.IOException: invalid constant type: 15 at 142
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:555)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 25 more
...
...
...
Caused by: java.io.IOException: invalid constant type: 15 at 142
    at javassist.bytecode.ConstPool.readOne(ConstPool.java:1044)
    at javassist.bytecode.ConstPool.read(ConstPool.java:984)
    at javassist.bytecode.ConstPool.<init>(ConstPool.java:125)
    at javassist.bytecode.ClassFile.read(ClassFile.java:770)
    at javassist.bytecode.ClassFile.<init>(ClassFile.java:114)
    at javassist.CtClassType.getClassFile2(CtClassType.java:191)
    ... 79 more

你的想法是正确的。

java.lang.RuntimeException: java.io.IOException: invalid constant type: 15 at 142 表明该应用程序在 java 8 方面存在问题 - 正如您在 link.

中所解释的那样

ma.glasnost.orika 映射器依赖于 javassist,如您在堆栈跟踪中所见。 这是orika的传递依赖。

您可以使用 mvn dependency:tree -verbose 构建依赖关系树。 在那里你可以查看什么库取决于 javassist 和确切的版本。

要使用与 Java 8 兼容的 javassist 版本,请使用该依赖项覆盖 orika 的隐式依赖项:

  <dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.18.2-GA</version>
  </dependency>

但对于此示例,最好将 orika 版本升级到 1.4.6,因为此版本已 Java 8 就绪。