# Using additional test types Sample

version 6.5-milestone-1

- Groovy DSL

- Kotlin DSL

| You can open this sample inside an IDE using the IntelliJ native importer or Eclipse Buildship.

This sample shows how to add additional test types to a JVM project in Gradle. Your project may have many different kinds of tests—​unit tests, integration tests, functional tests, etc. A JVM project in Gradle has a single `Test` task, but other tasks can be added to represent each of these test types.

In this sample, we are testing Java projects with JUnit4; however, this applies to other JVM languages as well.

For applications:

application/build.gradle

    plugins {
        id 'application'
    }

    dependencies {
        implementation project(':list')
        implementation project(':utilities')
        testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.1'
        testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
    }

    application {
        mainClass = 'org.gradle.sample.app.Main'
    }

    sourceSets {
        integrationTest {
            compileClasspath += sourceSets.main.output
            runtimeClasspath += sourceSets.main.output
        }
    }

    configurations {
        integrationTestImplementation.extendsFrom implementation
        integrationTestRuntimeOnly.extendsFrom runtimeOnly
    }

    dependencies {
        integrationTestImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.1'
        integrationTestRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
    }

    def integrationTest = tasks.register('integrationTest', Test) {
        description = 'Runs integration tests.'
        group = 'verification'

        testClassesDirs = sourceSets.integrationTest.output.classesDirs
        classpath = sourceSets.integrationTest.runtimeClasspath
        shouldRunAfter('test')
    }

    tasks.named('check') { dependsOn(integrationTest) }

application/build.gradle.kts

    plugins {
        application
    }

    dependencies {
        implementation(project(":list"))
        implementation(project(":utilities"))
        testImplementation("org.junit.jupiter:junit-jupiter-api:5.6.1")
        testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
    }

    application {
        mainClass.set("org.gradle.sample.app.Main")
    }

    sourceSets {
        create("integrationTest") {
            compileClasspath += sourceSets.main.get().output
            runtimeClasspath += sourceSets.main.get().output
        }
    }

    val integrationTestImplementation by configurations.getting {
        extendsFrom(configurations.implementation.get())
    }

    configurations["integrationTestRuntimeOnly"].extendsFrom(configurations.runtimeOnly.get())

    dependencies {
        integrationTestImplementation("org.junit.jupiter:junit-jupiter-api:5.6.1")
        "integrationTestRuntimeOnly"("org.junit.jupiter:junit-jupiter-engine")
    }

    val integrationTest = tasks.register<Test>("integrationTest") {
        description = "Runs integration tests."
        group = "verification"

        testClassesDirs = sourceSets["integrationTest"].output.classesDirs
        classpath = sourceSets["integrationTest"].runtimeClasspath
        shouldRunAfter("test")
    }

    tasks.check { dependsOn(integrationTest) }

For libraries:

utilities/build.gradle

    plugins {
        id 'java-library'
    }

    dependencies {
        implementation project(':list')
    }

    sourceSets {
        integrationTest {
            compileClasspath += sourceSets.main.output
            runtimeClasspath += sourceSets.main.output
        }
    }

    configurations {
        integrationTestImplementation.extendsFrom implementation
        integrationTestRuntimeOnly.extendsFrom runtimeOnly
    }

    dependencies {
        integrationTestImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.1'
        integrationTestRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
    }

    def integrationTest = tasks.register('integrationTest', Test) {
        description = 'Runs integration tests.'
        group = 'verification'

        testClassesDirs = sourceSets.integrationTest.output.classesDirs
        classpath = sourceSets.integrationTest.runtimeClasspath
        shouldRunAfter('test')
    }

    tasks.named('check') { dependsOn(integrationTest) }

utilities/build.gradle.kts

    plugins {
        `java-library`
    }

    dependencies {
        implementation(project(":list"))
    }

    sourceSets {
        create("integrationTest") {
            compileClasspath += sourceSets.main.get().output
            runtimeClasspath += sourceSets.main.get().output
        }
    }

    val integrationTestImplementation by configurations.getting {
        extendsFrom(configurations.implementation.get())
    }

    configurations["integrationTestRuntimeOnly"].extendsFrom(configurations.runtimeOnly.get())

    dependencies {
        integrationTestImplementation("org.junit.jupiter:junit-jupiter-api:5.6.1")
        "integrationTestRuntimeOnly"("org.junit.jupiter:junit-jupiter-engine")
    }

    val integrationTest = tasks.register<Test>("integrationTest") {
        description = "Runs integration tests."
        group = "verification"

        testClassesDirs = sourceSets["integrationTest"].output.classesDirs
        classpath = sourceSets["integrationTest"].runtimeClasspath
        shouldRunAfter("test")
    }

    tasks.check { dependsOn(integrationTest) }

These additional tests can be executed with the `integrationTest` task or as part of lifecycle `check`:

    $ ./gradlew check

    BUILD SUCCESSFUL
    5 actionable tasks: 5 executed

For more information, see section Configuring integration tests in the Testing in Java project chapter.