乐趣区

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

问题

在一个遗留的 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>
退出移动版