Home > database >  Mockito 4.6.1 does not work with springboot
Mockito 4.6.1 does not work with springboot

Time:07-01

When I run Junit tests from Eclipse it all passes, however when I run the test from a gradle task which is literally just a test task it fails with the following error message:

java.lang.IllegalStateException at PluginLoader.java:88
    Caused by: java.lang.IllegalStateException at DefaultMockitoPlugins.java:85
        Caused by: java.lang.reflect.InvocationTargetException at NativeConstructorAccessorImpl.java:-2
            Caused by: org.mockito.exceptions.base.MockitoInitializationException at Reporter.java:1131
                Caused by: java.lang.NoClassDefFoundError at SubclassInjectionLoader.java:34
                    Caused by: java.lang.ClassNotFoundException at BuiltinClassLoader.java:581

Here are my gradle plugins and dependencies:

plugins {
    id 'java-library'
    id 'project-report' 
    id 'org.springframework.boot' version '2.6.8'
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven-publish'

sourceCompatibility = 1.8
targetCompatibility = 1.8
    
dependencies {
        implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', {
            exclude module: "spring-boot-starter-jetty"
        }
        implementation group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
        implementation group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
        implementation group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat'
        implementation group: 'org.springframework.boot', name: 'spring-boot-starter-cache'
        implementation group: 'org.springframework.boot', name: 'spring-boot-starter-security'
        implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc'
    
        implementation group: 'org.springframework', name: 'spring-oxm', version: '5.3.20'
    
        implementation group: 'org.eclipse.persistence', name: 'org.eclipse.persistence.moxy', version: '2.7.9'
        implementation group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
        implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.9'
    
        implementation group: 'org.liquibase', name: 'liquibase-core', version: '4.8.0'
        implementation group: 'javax.cache', name: 'cache-api'  
        implementation group: 'org.ehcache', name: 'ehcache'
    
        implementation group: 'org.apache.commons', name: 'commons-text', version: '1.9'
        implementation group: 'net.iharder', name: 'base64', version: '2.3.9'
        implementation group: 'org.freemarker', name: 'freemarker', version: '2.3.31'
    
        //implementation group: 'com.nimbusds', name: 'nimbus-jose-jwt', version: '9.15.2'
        //implementation group: 'org.apache.cxf', name: 'cxf-rt-rs-security-jose', version: '3.4.5'
        implementation group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.4.15'
        implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.13', {
            exclude module: "commons-codec:commons-codec"
        }
        implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '5.2.2', {
            exclude module: "commons-codec:commons-codec"
        }
        implementation group: 'commons-codec', name: 'commons-codec', version: '1.15'   
        implementation group: 'javax.annotation', name: 'javax.annotation-api', version: '1.3.2'    
        implementation group: 'commons-io', name: 'commons-io', version: '2.11.0'
        implementation group: 'commons-cli', name: 'commons-cli', version: '1.5.0'
        implementation group: 'xerces', name: 'xercesImpl', version: '2.12.2'
    
        testImplementation group: 'com.h2database', name: 'h2'
        testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test', {
            exclude group: "org.junit.jupiter"
        }
        testImplementation group: 'org.springframework.security', name: 'spring-security-test'
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
        testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.1'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
        testImplementation group: 'org.hamcrest', name: 'hamcrest', version: '2.2'
        testImplementation group: 'org.mockito', name: 'mockito-core', version: '4.6.1'
        testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '2.23.0'
        testRuntimeOnly group: 'org.junit.platform', name: 'junit-platform-commons', version: '1.8.2'
        testImplementation group: 'org.junit.vintage', name: 'junit-vintage-engine', {
            exclude module: "org.hamcrest:hamcrest-core"
        }
        
        compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.22'
        testImplementation group: 'org.projectlombok', name: 'lombok', version: '1.18.22'
        testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'
        annotationProcessor 'org.projectlombok:lombok:1.18.22'
    }

If I delete testImplementation group: 'org.mockito', name: 'mockito-core', version: '4.6.1' it runs successfully, so it seems like somehow that dependency causes the issue, but unfortunately I definitely need that.

I have both JUnit 4 and JUnit 5 tests in my project. There are some test with @RunWith(SpringRunner.class) and some with @ExtendWith(MockitoExtension.class) annotations. I am using Java 11.

Any ideas are welcomed!

CodePudding user response:

You probably use this plugin.
apply plugin: 'io.spring.dependency-management' and this plugin fix the bytebuddy version to 1.11.22 where mockito 4.6.1 need 1.12.10.

So according to Could not initialize plugin: interface org.mockito.plugins.MockMaker after upgrading Mockito from 4.4.0 to 4.5.0

You need to add

    testImplementation 'net.bytebuddy:byte-buddy:1.12.10'

To override the version.

  • Related