单元测试不是由#gradle build"

时间:2014-10-17 19:59:04

标签: unit-testing gradle build.gradle

我很惊讶地注意到,当我跑步时,我的单元测试不再运行:

gradle build

为了运行我的单元测试,我需要明确发出gradle test

请注意,我最近修改了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时,有人可以帮助我查明阻止我的单元测试运行的原因吗?

2 个答案:

答案 0 :(得分:3)

我发现了观察到的行为的原因,即我的gradle构建没有运行单元测试:

问题很简单,考虑到我配置integrationTest任务的方式,特别是我添加的dependsOncheck.dependsOn integrationTest),我的集成测试在之前运行< / strong>单元测试,因为前者失败,所以从未达到单元测试。

(我现在需要找到一种方法让我的单元测试(test)任务在集成测试之前运行,但我想我会为此打开另一个问题。) < / p>

编辑:以下是它(集成测试前的单元测试)的实现方式:

check.dependsOn integrationTest
integrationTest.dependsOn test

答案 1 :(得分: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文件。它也应该有所帮助。