Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使单元测试作为集成测试运行_Java_Groovy_Gradle - Fatal编程技术网

Java 使单元测试作为集成测试运行

Java 使单元测试作为集成测试运行,java,groovy,gradle,Java,Groovy,Gradle,我有一些既有单元测试又有集成测试的项目。我将它们分开,以便单元测试作为常规构建的一部分运行,而集成测试仅由特定的“integTest”任务运行。这一切都很好 我有另一个项目,我没有写,也不能重构,它有一个单一的单元测试,而不是真正的单元测试。我有一个用于此项目的stock Gradle构建脚本,但我想添加我放在其他项目中的片段,以作为集成测试运行此测试。我还没有完成这项工作,但我认为我在其他项目中所做的工作只能完成一半。我确信它会让我以集成测试的形式运行该测试,但我还不知道如何使它不以单元测试的

我有一些既有单元测试又有集成测试的项目。我将它们分开,以便单元测试作为常规构建的一部分运行,而集成测试仅由特定的“integTest”任务运行。这一切都很好

我有另一个项目,我没有写,也不能重构,它有一个单一的单元测试,而不是真正的单元测试。我有一个用于此项目的stock Gradle构建脚本,但我想添加我放在其他项目中的片段,以作为集成测试运行此测试。我还没有完成这项工作,但我认为我在其他项目中所做的工作只能完成一半。我确信它会让我以集成测试的形式运行该测试,但我还不知道如何使它不以单元测试的形式运行

一个测试是在“src/test/java”中,我现在将它与我的“integTest”任务相关联(我以前使用过“src/integTest/groovy”,我想我也可以添加“src/integTest/java”)。如何从默认的“测试”任务中删除该目录,使“测试”从不运行任何测试

更新

尽管这篇文章的标题是关于将单元测试作为集成测试运行,但我只需要知道如何从单元测试通过中排除现有测试,答案是肯定的

看到这个标题的人可能想知道如何做到这一点,所以我将添加我如何做到这一点的细节

下面显示了我为实现此目的而添加到构建脚本中的所有内容:

// Without refactoring the source code of the project, this excludes the one test that looks like a unit test, but is
// actually an integration test.
test { exclude '**/*.*' }

sourceSets {
    integTest {
        java.srcDir file("src/test/java")
        resources.srcDir file("src/test/resources")
        runtimeClasspath = output + compileClasspath
    }
}

dependencies {
    integTestCompile sourceSets.main.output
    integTestCompile configurations.testCompile
    integTestCompile sourceSets.test.output
    integTestRuntime configurations.testRuntime
}

task integTest(type: Test) {
    testClassesDir  = sourceSets.integTest.output.classesDir
    classpath       = sourceSets.integTest.runtimeClasspath
    // This forces integration tests to always run if the task is run.
    outputs.upToDateWhen { false }
}

在build.gradle中,您可以执行以下操作:


试验{
排除“****”
}


或者通过添加行
test.enabled=false来禁用测试任务

您询问如何将
src/test/java
从被视为默认测试位置删除?是的,因为我希望我的integtest任务运行它,而不是默认测试任务。