Unsupported-majorminor-version原因一种及解决

40次阅读

共计 1298 个字符,预计需要花费 4 分钟才能阅读完成。

问题

在一个遗留的 Java web 项目中 (jdk7+spring4) 引入了 Mockito

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.3.3</version>
            <scope>test</scope>
        </dependency>

但是运行单元测试的时候就报错

Exception in thread "main" java.lang.UnsupportedClassVersionError: org/mockito/junit/MockitoJUnitRunner : Unsupported major.minor version 52.0

idea 中所有地方都配置的是 jdk7 怎么会有这个错呢

原因

rebuild 项目的时候 发现了如下的 Warning 信息

Warning:java: /Users/zhugw/.m2/repository/org/mockito/mockito-core/3.3.3/mockito-core-3.3.3.jar!/org/mockito/Answers.class: major version 52 is newer than 51, the highest major version supported by this compiler.
  It is recommended that the compiler be upgraded.

原来 Mockito-core-3.3.3.jar 本身就是由 jdk8 编译的

➜  .m2 javap -v -cp .//repository/org/mockito/mockito-core/3.3.3/mockito-core-3.3.3.jar org/mockito/Answers
Classfile jar:file:/Users/zhugw/.m2/repository/org/mockito/mockito-core/3.3.3/mockito-core-3.3.3.jar!/org/mockito/Answers.class
  Last modified Mar 13, 2020; size 2582 bytes
  MD5 checksum 001d1b92852a41c0f732f007b31d97ac
  Compiled from "Answers.java"
public final class org.mockito.Answers extends java.lang.Enum<org.mockito.Answers> implements org.mockito.stubbing.Answer<java.lang.Object>
  minor version: 0
  major version: 52

所以和 idea 的配置无关

解决

改用低版本

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.28.2</version>
            <scope>test</scope>
        </dependency>

正文完
 0