2014-10-17 42 views
3

我很惊讶地发现,我的单元测试不再运行时,我都跑一个gradle test明确。单元测试不是由“gradle这个构建”

请注意,我最近修改了我的build.gradle并增加了对集成测试的支持。我build.gradle如下:

buildscript { 
    def springBootVersion = "1.2.0.BUILD-SNAPSHOT" 

    repositories { 
     mavenCentral() 
     maven { url "http://repo.spring.io/release" } 
     maven { url "http://repo.spring.io/milestone" } 
     maven { url "http://repo.spring.io/snapshot" } 
    } 
    dependencies {  
     classpath ("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") 
    } 
} 

apply plugin: 'java' 
apply plugin: 'spring-boot' 

sourceSets { 
    generated { 
     java { 
      srcDirs = ['build/generated-sources/java'] 
     } 
    } 

    integrationTest { 
     java.srcDir file('src/it/java') 
     resources.srcDir file('src/it/resources') 
     compileClasspath = sourceSets.main.output + configurations.testRuntime 
     runtimeClasspath = output + compileClasspath 
    } 
} 

configurations { 
    querydslapt 
} 

dependencies { 
    def springSecurityVersion = "4.0.0.M2" 
    def springBootVersion = "1.2.0.BUILD-SNAPSHOT" 
    def springSessionVersion = "1.0.0.BUILD-SNAPSHOT" 
    def hibernateVersion = "4.3.6.Final" 
    def javaxMailVersion = "1.4.7" 
    def queryDslVersion = "3.4.3" 
    def thymeleafTilesVersion = "2.1.1.RELEASE" 
    def apacheCommonsLang = "3.1" 
    def javaxValidationApi = "1.1.0.Final" 
    def javaxActivation = "1.1.1" 
    def commonsCodec = "1.5" 
    def jstl = "1.2" 
    def hibernateJpa = "1.0.0.Final" 
    def cglibNodep = "2.2.2" 
    def commonsIo = "2.4" 
    def javaxTransactionJta = "1.1" 
    def jacksonHibernate4 = "2.3.2" 

    def mockitoVersion = "1.9.5" 
    def hamcrestVersion = "1.3" 
    def festVersion = "1.4" 
    def gatlingVersion = "2.0.1" 

    compile project(":bignibou-client") 

    compile("log4j:log4j") 
    compile("org.springframework.boot:spring-boot-starter") 
    compile("org.springframework.boot:spring-boot-starter-web") 
    compile("org.springframework.boot:spring-boot-starter-tomcat") 
    compile("org.apache.commons:commons-lang3:${apacheCommonsLang}") 
    compile("org.springframework:spring-core") 
    compile("org.springframework:spring-beans") 
    compile("javax.validation:validation-api:${javaxValidationApi}") 
    compile("org.hibernate:hibernate-validator") 
    compile("org.springframework:spring-context-support") 
    compile("javax.mail:mail:${javaxMailVersion}") 
    compile("javax.activation:activation:${javaxActivation}") 
    compile("joda-time:joda-time") 
    compile("commons-codec:commons-codec:${commonsCodec}") 
    compile("org.springframework.security:spring-security-core:${springSecurityVersion}") 
    compile("org.springframework.security:spring-security-config:${springSecurityVersion}") 
    compile("org.springframework.security:spring-security-web:${springSecurityVersion}")  
    compile("org.thymeleaf:thymeleaf-spring4")  
    compile("org.thymeleaf.extras:thymeleaf-extras-tiles2:${thymeleafTilesVersion}") 
    compile("org.thymeleaf.extras:thymeleaf-extras-tiles2-spring4:${thymeleafTilesVersion}") 
    compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity3") 
    compile("javax.servlet:jstl:${jstl}") 
    compile("mysql:mysql-connector-java") 
    compile("org.hibernate:hibernate-core:${hibernateVersion}") 
    compile("org.hibernate:hibernate-entitymanager") 
    compile("org.hibernate.javax.persistence:hibernate-jpa-2.1-api:${hibernateJpa}") 
    compile("commons-collections:commons-collections") 
    compile("cglib:cglib-nodep:${cglibNodep}") 
    compile("javax.transaction:jta:${javaxTransactionJta}") 
    compile("org.springframework:spring-jdbc") 
    compile("org.springframework:spring-orm") 
    compile("com.zaxxer:HikariCP") 
    compile("commons-pool:commons-pool") 
    compile("org.thymeleaf:thymeleaf") 
    compile("org.springframework.data:spring-data-commons") 
    compile("org.springframework.data:spring-data-jpa") 
    compile("com.mysema.querydsl:querydsl-core:${queryDslVersion}") 
    compile("com.mysema.querydsl:querydsl-jpa:${queryDslVersion}") 
    compile("org.springframework:spring-aspects") 
    compile("commons-io:commons-io:${commonsIo}") 
    compile("com.fasterxml.jackson.core:jackson-core") 
    compile("com.fasterxml.jackson.core:jackson-annotations") 
    compile("com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:${jacksonHibernate4}") 
    compile("org.springframework:spring-tx") 
    compile("javax.cache:cache-api") 
    compile("org.springframework.data:spring-data-redis") 
    compile("redis.clients:jedis") 
    compile("org.springframework.session:spring-session:${springSessionVersion}") 
    compile("org.springframework.cloud:spring-cloud-spring-service-connector") 
    compile("org.springframework.cloud:spring-cloud-heroku-connector") 
    compile("org.springframework.cloud:spring-cloud-localconfig-connector") 

    querydslapt "com.mysema.querydsl:querydsl-apt:${queryDslVersion}" 

    testCompile("junit:junit") 
    testCompile("org.springframework:spring-test") 
    testCompile("org.jbehave.web:jbehave-web-selenium:3.5.5") 
    testCompile("org.jbehave:jbehave-core:3.8") 
    testCompile("org.jbehave:jbehave-spring:3.7.5") 
    testCompile("org.mockito:mockito-all:${mockitoVersion}") 
    testCompile("org.hamcrest:hamcrest-core:${hamcrestVersion}") 
    testCompile("org.hamcrest:hamcrest-integration:${hamcrestVersion}") 
    testCompile("org.hamcrest:hamcrest-all:${hamcrestVersion}") 
    testCompile("org.easytesting:fest-assert:${festVersion}") 
    testCompile("org.hsqldb:hsqldb") 
    testCompile("org.hamcrest:hamcrest-library") 
    testCompile("io.gatling.highcharts:gatling-charts-highcharts:${gatlingVersion}") 
    testCompile("org.springframework.security:spring-security-test:${springSecurityVersion}") 
} 

task generateQueryDSL(type: JavaCompile, group: 'build', description: 'Generates the QueryDSL query types') { 
    source = sourceSets.main.java 
    classpath = configurations.compile + configurations.querydslapt 
    options.compilerArgs = [ 
      "-proc:only", 
      "-processor", "com.mysema.query.apt.jpa.JPAAnnotationProcessor" 
    ] 
    destinationDir = sourceSets.generated.java.srcDirs.iterator().next() 
} 

test { 
    reports.html.destination = file("$reports.html.destination/unit") 
    reports.junitXml.destination = file("$reports.junitXml.destination/unit") 
} 

task integrationTest(type: Test) { 
    testClassesDir = sourceSets.integrationTest.output.classesDir 
    classpath = sourceSets.integrationTest.runtimeClasspath 
    reports.html.destination = file("$reports.html.destination/integration") 
    reports.junitXml.destination = file("$reports.junitXml.destination/integration") 
} 

compileJava { 
    dependsOn generateQueryDSL 
    source generateQueryDSL.destinationDir 
} 

compileGeneratedJava { 
    dependsOn generateQueryDSL 
    options.warnings = false 
    classpath += sourceSets.main.runtimeClasspath 
} 

check.dependsOn integrationTest 

clean { 
    delete sourceSets.generated.java.srcDirs 
} 

repositories { 
    mavenLocal() 
    mavenCentral() 
    maven { url "http://repo.spring.io/release" } 
    maven { url "http://repo.spring.io/milestone" } 
    maven { url "http://repo.spring.io/snapshot" } 
} 

有人可以帮我找出是什么阻止我的单元测试从正在运行时,我发出:gradle build

+1

我不能发现任何明显的。第一步是将该脚本简化为最小可重现的示例。 – 2014-10-17 23:52:13

+0

彼得你好。我正在制作一个样本...忍受着我。 – balteo 2014-10-18 10:59:27

回答

2

我已经找到了观察到的行为,即单元测试不是由我gradle这个编译运行的原因:

这个问题很简单,就是给我配置的方式我integrationTest任务,尤其是dependsOn我已经加入(check.dependsOn integrationTest),我的集成测试在单元测试之前运行,并且由于前者失败,单元测试从未达到。

(我现在在某种程度上需要找到一种方式,我的单元测试(test)任务的集成测试之前运行,但我想我会打开另一个问题。)

编辑:这里是如何(集成测试之前的单元测试)可以实现:

check.dependsOn integrationTest 
integrationTest.dependsOn test 
+3

实现此目标的更好方法是'integrationTest.shouldRunAfter(test)'。 – 2014-10-18 20:29:39

+1

感谢这个提示彼得。对于那些感兴趣的“shoulRunAfter”记录[这里](http://www.gradle.org/docs/current/userguide/more_about_tasks.html#sec:ordering_tasks) – balteo 2014-10-19 07:52:49

0

当我配置的集成测试前段时间以下配置工作对我来说:

sourceSets { 
    integrationTest.java.srcDirs = ['src/it/java'] 
} 

dependencies { 
    integrationTestCompile sourceSets.main.output 
    integrationTestCompile configurations.testCompile 
    integrationTestCompile sourceSets.test.output 
    integrationTestRuntime configurations.testRuntime 
} 

task integrationTest(type: Test) { 
    testClassesDir = sourceSets.integrationTest.output.classesDir 
    classpath = sourceSets.integrationTest.runtimeClasspath 
    reports.html.destination = file("$reports.html.destination/integration") 
    reports.junitXml.destination = file("$reports.junitXml.destination/integration") 
} 

check.dependsOn integrationTest 

integrationTest任务名称是与build.gradle文件有效。

您还可以导航到$GRADLE_HOME/samples/java/withIntegrationTests并调查目前存在的build.gradle文件。它也应该是有帮助的。

+0

感谢您的回复。我是Gradle/Groovy新手,我需要了解当前配置有什么问题。你是否遇到与我一样的问题,即需要明确运行gradle测试命令? – balteo 2014-10-17 20:38:04

+0

不,测试在执行'gradle build'时运行。正如彼得评论你的帖子,提供一个SSCCE。 – Opal 2014-10-18 07:16:52