问题

在一个遗留的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>

zhuguowei2
825 声望26 粉丝