Unit tests not executed by "gradle build" command

I was surprised to notice that my unit test no longer runs on startup:

gradle build

      

For my unit tests to run, I need to explicitly specify gradle test

.

Note that I recently changed mine build.gradle

and added support for integration tests. Mine build.gradle

looks like this:

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" }
}

      

Can someone please help me to identify what is preventing my unit tests from running when I issue gradle build

:?

+3


source to share


2 answers


I found the reason for the observable behavior, i.e. unit tests not being executed by my gradle construct:

The problem is that given how I configured my task integrationTest

, and especially the dependsOn

one I added ( check.dependsOn integrationTest

), my integration tests were running before the unit test and since the former were in a state of failure, the unit tests were never achieved.

(I now need to find a way to do my unit test ( test

) task before testing the integration, but I think I'll open another question for that.)



edit . Here's how to do it (unit tests before integration tests):

check.dependsOn integrationTest
integrationTest.dependsOn test

      

+2


source


When I was setting up integration tests a while ago, the following configuration worked for me:

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

the task name acts with your build.gradle

file.

You can also go to $GRADLE_HOME/samples/java/withIntegrationTests

and examine the file that exists there build.gradle

. This should also be helpful.

0


source







All Articles