You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法导入Mockito库至测试类,编译依赖解析失败求助

解决Mockito依赖未解析的编译错误问题

我在Java项目的build.gradle中已经配置了Mockito和JUnit Jupiter的测试依赖,但测试类无法导入Mockito相关类(比如MockitoExtension、Mock等),出现依赖未解析的编译错误。执行./gradlew build命令时,测试类仍然存在编译问题。

项目结构说明

测试类位于src/test/java/com/codingame/service目录下,主代码位于src/main/java/com/codingame目录下。

当前build.gradle代码

plugins {
    id 'java'
    id 'application'
}
ext {
    buildId = System.currentTimeMillis()
    codinGameMainClass = "com.codingame.App"
}
group = 'com.coding-game-problem'
version = '0.0.1-SNAPSHOT'
java {
    sourceCompatibility = '8'
}

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    testImplementation 'org.mockito:mockito-junit-jupiter:4.11.0'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
}

test {
    reports {
        junitXml.enabled = true
        def destinationDirectory = System.getProperty("user.home") + "/.gradle/daemon/${buildId}/test-results"
        junitXml.destination = file(destinationDirectory)
        html.enabled = true
    }
    useJUnitPlatform {
    }
    testLogging.events ("STANDARD_ERROR")
    testLogging.events ("STANDARD_OUT")
    afterSuite { desc, result ->
        if (!desc.parent) {
            println "\nTEST RESULT: ${result.resultType}"
            println "TEST SUMMARY: RAN ${result.testCount} TESTS, " +
                    "${result.successfulTestCount} SUCCEEDED, " +
                    "${result.failedTestCount} FAILED, " +
                    "${result.skippedTestCount} SKIPPED"
        }
    }
}
tasks.named('test') {
    useJUnitPlatform()
}

测试类中无法导入的代码片段

import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@DisplayName("ContestServiceTest")
@ExtendWith(MockitoExtension.class)

解决方案

  • 清理仓库配置:jcenter已停止服务,建议从repositories中移除,仅保留mavenCentral(),避免依赖解析失败。
  • 移除重复配置:删除tasks.named('test')闭包中的useJUnitPlatform(),因为test闭包中已配置该属性,重复配置可能引发冲突。
  • 刷新依赖缓存:执行命令 ./gradlew clean build --refresh-dependencies,强制重新下载依赖,修复缓存损坏问题。
  • 验证依赖引入:执行命令 ./gradlew dependencies --configuration testImplementation,检查输出中是否包含org.mockito:mockito-junit-jupiter:4.11.0及org.mockito:mockito-core,确认依赖已正确解析。
  • 检查Gradle版本:Java 8环境配合Mockito 4.x,建议使用Gradle 7.0及以上版本,若版本过低请升级Gradle。

修改后的build.gradle示例

plugins {
    id 'java'
    id 'application'
}
ext {
    buildId = System.currentTimeMillis()
    codinGameMainClass = "com.codingame.App"
}
group = 'com.coding-game-problem'
version = '0.0.1-SNAPSHOT'
java {
    sourceCompatibility = '8'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.mockito:mockito-junit-jupiter:4.11.0'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
}

test {
    reports {
        junitXml.enabled = true
        def destinationDirectory = System.getProperty("user.home") + "/.gradle/daemon/${buildId}/test-results"
        junitXml.destination = file(destinationDirectory)
        html.enabled = true
    }
    useJUnitPlatform()
    testLogging.events ("STANDARD_ERROR")
    testLogging.events ("STANDARD_OUT")
    afterSuite { desc, result ->
        if (!desc.parent) {
            println "\nTEST RESULT: ${result.resultType}"
            println "TEST SUMMARY: RAN ${result.testCount} TESTS, " +
                    "${result.successfulTestCount} SUCCEEDED, " +
                    "${result.failedTestCount} FAILED, " +
                    "${result.skippedTestCount} SKIPPED"
        }
    }
}

内容的提问来源于stack exchange,提问作者Manish Verma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 17:00:33