使用从Jenkins或任何其他CI服务器上的Gradle插件运行的Spring Boot服务器执行Cucumber集成测试

时间:2017-11-03 06:38:14

标签: spring-boot cucumber-jvm gradle-plugin cucumber-java cucumber-junit

我有一个用Gradle构建的spring boot微服务项目。

我想在代码签入后立即在Jenkins或CI服务器上执行黄瓜集成测试。每次代码签入后,CI服务器上的构建作业都会自动触发。这份工作叫我 gradle build

我可以从我的STS或eclipse执行黄瓜测试用例作为普通的JUnit测试用例,嵌入式tomcat服务器启动,在黄瓜测试用例上执行后,服务器停止运行。

这正是我想要的gradle构建:

如何通过gradle.build在正在运行的spring boot嵌入式服务器上执行黄瓜集成测试?

当前行为:当在CI服务器上调用 gradle.build 时,黄瓜集成测试只是在没有目标spring boot服务器的情况下调用。

预期的行为:在CI服务器上调用 gradle.build 后,黄瓜测试应该在正在运行的Spring boot嵌入式服务器上执行,并且应该停止黄瓜测试案例执行后本身。

注意:我在项目中为它创建了一个特殊的cucumber-test个人资料,并有自己的配置文件

我的gradle.build看起来像是:

buildscript {
    ext {
        springBootVersion = '1.4.2.RELEASE'
    }
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

jar {
    manifest {
        attributes 'Main-Class': 'com.pa.omas.Main'
    }
    baseName = 'omas'
    version = ''
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
    maven { url "http://smartbearsoftware.com/repository/maven2" }
}

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

configurations {
    integrationTestCompile.extendsFrom testCompile
    integrationTestRuntime.extendsFrom testRuntime
    cucumberRuntime {
        extendsFrom testRuntime
    }
}

task copyScripts(type: Copy) {
    from("scripts")
    into("build/libs")   
}

task copyReports(type: Copy) {
    from("reports")
    into("build/libs/reports")   
}

task integrationTest(type: Test) {
    testClassesDir = sourceSets.integrationTest.output.classesDir
    classpath = sourceSets.integrationTest.runtimeClasspath
    outputs.upToDateWhen { false }
}

check.dependsOn integrationTest
integrationTest.mustRunAfter test

tasks.withType(Test) {
    reports.html.destination = file("${reporting.baseDir}/${name}")
}

bootRun { 
    args = ["--spring.profiles.active=cucumber-test"] 
}

task cucumber(){
    dependsOn assemble, compileTestJava

    doLast {
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['-p', 'pretty', '--monochrome', '-p', 'html:reports/cucumber/cucumber-html-reports', '-p', 'junit:reports/cucumber-junit/cucumber-junit-report.xml',
                    '-p', 'html:reports/cucumber', '--glue', 'src/integration-test/java/com/pa/omas/cucumber', 'src/integration-test/resources']
        }
        copyReports
    }
}

build {
   dependsOn copyScripts
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.cloud:spring-cloud-starter-stream-kafka')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.projectlombok:lombok')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('ch.qos.logback:logback-classic')
    compile('org.mariadb.jdbc:mariadb-java-client:1.5.4')
    compile('org.hibernate:hibernate-java8')
    compile 'com.puppycrawl.tools:checkstyle:8.3'
    compile group: 'net.masterthought', name: 'cucumber-reporting', version: '3.11.0'
    compile group: 'net.masterthought', name: 'maven-cucumber-reporting', version: '3.11.0'

    compile group: 'com.zaxxer', name: 'HikariCP', version: '2.6.3'
    compile('com.fasterxml.jackson.datatype:jackson-datatype-jsr310')
    compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.7.0'
    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.7.0'

    testCompile('com.h2database:h2')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile group: 'org.mockito', name: 'mockito-all', version: '2.0.2-beta'
    testCompile group: 'info.cukes', name: 'cucumber-junit', version: '1.2.5'
    testCompile group: 'info.cukes', name: 'cucumber-spring', version: '1.2.5'

    integrationTestCompile('com.h2database:h2')
    integrationTestCompile('org.springframework.boot:spring-boot-starter-test')
    integrationTestCompile 'info.cukes:cucumber-java:1.2.5'
    integrationTestCompile 'junit:junit:4.12'
    integrationTestCompile group: 'info.cukes', name: 'cucumber-junit', version: '1.2.5'
    integrationTestCompile group: 'info.cukes', name: 'cucumber-spring', version: '1.2.5'
    integrationTestCompile group: 'info.cukes', name: 'cucumber-java', version: '1.2.5'
    integrationTestCompile group: 'info.cukes', name: 'cucumber-core', version: '1.2.5'
    integrationTestCompile group: 'info.cukes', name: 'cucumber-html', version: '0.2.6'
    integrationTestCompile group: 'info.cukes', name: 'cucumber-jvm-deps', version: '1.0.5'
    integrationTestCompile group: 'info.cukes', name: 'gherkin', version: '2.12.2'
    integrationTestCompile group: 'io.cucumber', name: 'gherkin', version: '5.0.0'
    integrationTestCompile group: 'info.cukes', name: 'cucumber-java8', version: '1.2.5'
    integrationTestCompile group: 'org.webjars.npm', name: 'gherkin', version: '4.1.3'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Camden.SR3"
    }
}

我的 project structure 看起来像是:

Project Structure

StepsDefinitionAnnotations

StepsDefinitionAnnotations

CucumberTest CucumberTest

非常感谢!!

1 个答案:

答案 0 :(得分:0)

最后,我找到了解决这个问题的方法。

黄瓜cli主类无法找到stepsDefinition文件所在的testRuntime。因此,它只是调用黄瓜测试用例而不是执行它们。

我所做的只包括主要&的 sourceSets 在gradle文件中测试,linke如下:

sourceSets {
    main {
        java {
            srcDirs = ["src/main/java"]
        }
    }
    test {
        java {
            srcDirs = ["src/test/"]
        }
    }
}

它对我来说很好。

非常感谢。希望这也有助于其他人!!