java - Gradle 不执行测试

标签 java gradle junit junit5

当我运行我的 gradle 脚本时,它总是这样做:

> Configure project :
Evaluating root project 'trading-library' using build file 'C:\Users\xxx\Documents\Repositories\Trading_Library\Trading_Library\build.gradle'.
Invalidating in-memory cache of C:\Users\xxx\.gradle\caches\journal-1\file-access.bin
All projects evaluated.
Selected primary task 'cleanTest' from project :
Selected primary task 'test' from project :
Tasks to be executed: [task ':cleanTest', task ':compileJava', task ':processResources', task ':classes', task ':compileTestJava', task ':processTestResources', task ':testClasses', task ':test']
:cleanTest (Thread[Execution worker for ':',5,main]) started.

> Task :cleanTest UP-TO-DATE
Task ':cleanTest' is not up-to-date because:
  Task has not declared any outputs despite executing actions.
:cleanTest (Thread[Execution worker for ':',5,main]) completed. Took 0.002 secs.
:compileJava (Thread[Execution worker for ':',5,main]) started.

> Task :compileJava UP-TO-DATE
Skipping task ':compileJava' as it is up-to-date.
:compileJava (Thread[Execution worker for ':',5,main]) completed. Took 0.029 secs.
:processResources (Thread[Execution worker for ':',5,main]) started.

> Task :processResources NO-SOURCE
Skipping task ':processResources' as it has no source files and no previous output files.
:processResources (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.
:classes (Thread[Execution worker for ':',5,main]) started.

> Task :classes UP-TO-DATE
Skipping task ':classes' as it has no actions.
:classes (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.
:compileTestJava (Thread[Execution worker for ':',5,main]) started.

> Task :compileTestJava UP-TO-DATE
Skipping task ':compileTestJava' as it is up-to-date.
:compileTestJava (Thread[Execution worker for ':',5,main]) completed. Took 0.032 secs.
:processTestResources (Thread[Execution worker for ':',5,main]) started.

> Task :processTestResources NO-SOURCE
Skipping task ':processTestResources' as it has no source files and no previous output files.
:processTestResources (Thread[Execution worker for ':',5,main]) completed. Took 0.001 secs.
:testClasses (Thread[Execution worker for ':',5,main]) started.

> Task :testClasses UP-TO-DATE
Skipping task ':testClasses' as it has no actions.
:testClasses (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.
:test (Thread[Execution worker for ':',5,main]) started.

> Task :test
Task ':test' is not up-to-date because:
  Output property 'binResultsDir' file C:\Users\xxx\Documents\Repositories\Trading_Library\Trading_Library\build\test-results\test\binary has been removed.
  Output property 'binResultsDir' file C:\Users\xxx\Documents\Repositories\Trading_Library\Trading_Library\build\test-results\test\binary\output.bin has been removed.
  Output property 'binResultsDir' file C:\Users\xxx\Documents\Repositories\Trading_Library\Trading_Library\build\test-results\test\binary\output.bin.idx has been removed.
Custom actions are attached to task ':test'.
Finished generating test XML results (0.0 secs) into: C:\Users\xxx\Documents\Repositories\Trading_Library\Trading_Library\build\test-results\test
Generating HTML test report...
Finished generating test html results (0.017 secs) into: C:\Users\xxx\Documents\Repositories\Trading_Library\Trading_Library\build\reports\tests\test
:test (Thread[Execution worker for ':',5,main]) completed. Took 0.367 secs.

这是 gradle clean build 后的输出。所以看起来 gradle 没有找到源代码并且 gradle 也没有执行测试。没有生成测试报告,测试结果 html 文件没有显示任何已执行的测试。

这是我的gradle文件:
buildscript {
    repositories {
        maven {
            url "${nexusUrl}/content/groups/public/"
            credentials {
                username "$nexusUsername"
                password "$nexusPassword"
            }
        }
    }

    dependencies {
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6"
    }
}


apply plugin: 'maven'
apply plugin: 'java'
apply plugin: "org.sonarqube"
apply plugin: "jacoco"

jar {
    baseName = 'xxx-trading-lib'
    version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 11
targetCompatibility = 11

repositories {
    maven {
        url "${nexusUrl}/content/groups/public/"
        credentials {
            username "$nexusUsername"
            password "$nexusPassword"
        }
    }
}


group 'de.xxx.libraries'
version '1.0-SNAPSHOT'

sourceCompatibility = 11
targetCompatibility = 11


dependencies {
    compileOnly 'org.projectlombok:lombok:1.18.8'
    annotationProcessor 'org.projectlombok:lombok:1.18.8'

    compile group: 'com.google.guava', name: 'guava', version: '28.0-jre'
    compile group: 'org.joda', name: 'joda-money', version: '1.0.1'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.25'

    testCompile group: 'org.assertj', name: 'assertj-core', version: '3.12.2'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.0'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.0'


}

task sourcesJar(type: Jar, dependsOn:classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    archives sourcesJar
}

uploadArchives {
    repositories {
        mavenDeployer {

            repository(url: "${nexusUrl}/content/repositories/xxx-trading-release") {
                pom.artifactId = 'xxx-trading-release'
                authentication(userName: nexusUsername, password: nexusPassword)

                pom.version='0.0.1-RELEASE'
                pom.artifactId='xxx-trading-release'
                pom.groupId ='de.xxx.components'

            }


            snapshotRepository(url: "${nexusUrl}/content/repositories/xxx-trading-snapshot") {
                pom.artifactId = 'xxx-trading-snapshot'
                authentication(userName: nexusUsername, password: nexusPassword)

                pom.version='0.0.1-SNAPSHOT'
                pom.artifactId='xxx-trading-snapshot'
                pom.groupId ='de.xxx.components'
            }
        }
    }
}

我还删除了 build 文件夹并检查是否会生成类文件,这工作正常。之后有新的类文件。有谁知道我在这里做错了什么?我正在使用 gradle 5.2.1 和 java 11。

编辑:它似乎中断了,因为它找不到 java.lang.object:
23:10:45.359 [DEBUG] [TestEventLogger] Gradle Test Run :test STARTED
23:10:45.370 [DEBUG] [org.gradle.api.internal.tasks.testing.detection.AbstractTestFrameworkDetector] test-class-scan : failed to scan parent class java/lang/Object, could not find the class file
23:10:45.371 [DEBUG] [org.gradle.api.internal.tasks.testing.detection.AbstractTestFrameworkDetector] test-class-scan : failed to scan parent class java/lang/Object, could not find the class file
23:10:45.371 [DEBUG] [TestEventLogger]
23:10:45.371 [DEBUG] [TestEventLogger] Gradle Test Run :test PASSED 

最佳答案

您必须在测试配置中启用 JUnit 支持。对于 JUnit 5,将以下内容添加到您的 gradle 构建文件中:

test {
    useJUnitPlatform()
}

关于java - Gradle 不执行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56960467/

相关文章:

java - 2016 年,我们是否还需要使用 BouncyCaSTLe 来解决美国政府对密码学实现的导出管制限制?

java - 解释套接字响应的好方法

java - Vert.x 失败到 "Run your module and see your changes immediately"

android - flutter 发布apk错误:Execution failed for task ':app:lintVitalRelease'

java - JUnit 测试 System.err.print()

java - spring boot war部署到tomcat给出404错误

java - 无法在 eclipse 中使用 tomcat 服务器适配器链接 tomee plus

git - 运行exec任务作为配置阶段的输入

java - 在@before方法中保存状态,进行@test,然后回滚@after中的更改

java - 运行 junit 测试时出现 ZipException