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

IntelliJ无法自动构建测试,需每次执行./gradlew tests的解决方法

问题描述

在IntelliJ中运行新测试时,系统提示测试不存在并抛出如下错误:

Internal Error occurred.
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
    at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:189)
    at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:168)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
...

旧测试原本能正常执行,但修改测试内容为如下代码后,IntelliJ运行仍显示测试通过:

@Test
fun `the test`() {
    assertTrue(false)
}

执行./gradlew test命令后,测试能正常运行,IntelliJ也暂时恢复正常,但修改测试或新增测试后,IntelliJ仍显示上次gradlew执行的结果,必须重新执行该命令才能生效。

需求:解决该问题,让IntelliJ在运行测试前自动构建测试。


附:build.gradle.kts文件内容:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.6.10"
}

group = "pt.isel.pc"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

val ktlint: Configuration by configurations.creating

dependencies {

    implementation("org.slf4j:slf4j-api:1.7.36")
    implementation("org.slf4j:slf4j-simple:2.0.0-alpha7")

    testImplementation(kotlin("test"))

    ktlint("com.pinterest:ktlint:0.48.2") {
        attributes {
            attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL))
        }
    }
}

tasks.test {
    useJUnitPlatform()
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = "17"
}

val outputDir = "${project.buildDir}/reports/ktlint/"
val inputFiles = project.fileTree(mapOf("dir" to "src", "include" to "**/*.kt"))
val ktlintCheck by tasks.creating(JavaExec::class) {
    inputs.files(inputFiles)
    outputs.dir(outputDir)

    description = "Check Kotlin code style."
    classpath = ktlint
    mainClass.set("com.pinterest.ktlint.Main")
    args = listOf("src/**/*.kt")
}

tasks.named("check") {
    dependsOn("ktlintCheck")
}
解决方法

1. 配置IntelliJ使用Gradle处理测试

打开IntelliJ的Settings/Preferences(Windows/Linux按Ctrl+Alt+S,Mac按Cmd+,),进入Build, Execution, Deployment > Build Tools > Gradle:

  • 将Build and run using和Run tests using均设置为Gradle,让IntelliJ完全依赖Gradle的构建和测试流程,避免IDE自身缓存与Gradle不同步。
  • 点击右上角的Gradle同步按钮(大象图标),或按Ctrl+Shift+O(MacCmd+Shift+O)重新同步项目。

2. 强制运行测试前构建项目

打开测试的运行配置(点击运行按钮旁的配置下拉菜单,选择Edit Configurations):

  • 找到对应的JUnit测试配置,在Configuration标签页中勾选Build project before running,确保每次运行测试前自动触发项目构建。

3. 清理缓存

  • 清理IntelliJ缓存:点击File > Invalidate Caches...,选择Invalidate and Restart,重启IDE后重新同步Gradle。
  • 清理Gradle缓存:执行命令./gradlew clean,随后重新同步项目。

4. 确认依赖与配置正确性

  • 确保testImplementation(kotlin("test"))依赖已正确引入,该依赖包含JUnit 5核心组件,是IntelliJ识别测试的基础。
  • 确认tasks.test { useJUnitPlatform() }配置存在且正确,保证Gradle与IDE使用相同的JUnit测试引擎。

内容的提问来源于stack exchange,提问作者Will Potato

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 03:32:12