向Gradle添加其他测试套件

我正在尝试将Gradle(1.4)添加到具有多个测试套件的现有项目中。 位于src/test/java的标准unit testing成功运行,但是我无法设置任务来运行位于src/integration-test/java的JUnit测试。

当我运行gradle intTest我得到几个在src/main cannot find symbol类的cannot find symbol错误。 这让我相信依赖关系没有正确设置。 如何设置intTest以便它将运行我的JUnit集成测试?

的build.gradle

 apply plugin: 'java' sourceCompatibility = JavaVersion.VERSION_1_6 sourceSets { integration { java { srcDir 'src/integration-test/java' } resources { srcDir 'src/integration-test/resources' } } } dependencies { compile(group: 'org.springframework', name: 'spring', version: '3.0.7') testCompile(group: 'junit', name: 'junit', version: '4.+') testCompile(group: 'org.hamcrest', name: 'hamcrest-all', version: '1.+') testCompile(group: 'org.mockito', name: 'mockito-all', version: '1.+') testCompile(group: 'org.springframework', name: 'spring-test', version: '3.0.7.RELEASE') integrationCompile(group: 'junit', name: 'junit', version: '4.+') integrationCompile(group: 'org.hamcrest', name: 'hamcrest-all', version: '1.+') integrationCompile(group: 'org.mockito', name: 'mockito-all', version: '1.+') integrationCompile(group: 'org.springframework', name: 'spring-test', version: '3.0.7.RELEASE') } task intTest(type: Test) { testClassesDir = sourceSets.integration.output.classesDir classpath += sourceSets.integration.runtimeClasspath } 

细节: Gradle 1.4

解决方案:我没有为集成测试源集设置编译类路径(见下文)。 在我的I代码中,我将编译类路径设置为sourceSets.test.runtimeClasspath以便我没有“integrationCompile”的重复依赖项

 sourceSets { integrationTest { java { srcDir 'src/integration-test/java' } resources { srcDir 'src/integration-test/resources' } compileClasspath += sourceSets.main.runtimeClasspath } } 

“集成”sourceSet尚未配置其编译和运行时类路径。 这就是为什么它无法从您的主要源集中找到类。 您可以通过以下方式配置编译和运行时类路径:

 sourceSets { integTest { java.srcDir file('src/integration-test/java') resources.srcDir file('src/integration-test/resources') compileClasspath = sourceSets.main.output + configurations.integTest runtimeClasspath = output + compileClasspath } } 

在大多数情况下,您希望使用与unit testing相同的依赖项以及一些新的依赖项。 这将在现有的unit testing之上添加unit testing的依赖关系以进行集成测试(如果有的话)。

 sourceSets { integrationTest { compileClasspath += sourceSets.test.compileClasspath runtimeClasspath += sourceSets.test.runtimeClasspath } }