[Gradle] Creating a new source set for Integration Test using Kotlin DSL

Jay Kim
1 min readJul 22, 2023

Here is the motivation on why a new source set was added:

  • Have a source directory to differentiate from unit tests and integration tests.
  • Have an ability to run unit tests and integration tests at different stages as we focus highly on unit tests when working on a feature. The reason behind that is because integration tests are more expensive than unit tests.
plugins {
java
id("org.springframework.boot") version "3.1.1"
id("io.spring.dependency-management") version "1.1.0"
}

group = "io.jay"
version = "0.0.1-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_17
}

repositories {
mavenCentral()
}

dependencies {
implementation("org.springframework.boot:spring-boot-starter-actuator")
implementation("org.springframework.boot:spring-boot-starter-webflux")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
testImplementation("com.squareup.okhttp3:okhttp")
testImplementation("com.squareup.okhttp3:mockwebserver")
}

tasks.withType<Test> {
useJUnitPlatform()

testLogging {
events("passed", "skipped", "failed")
}
}

val integrationTest: SourceSet = sourceSets.create("integrationTest") {
java {
compileClasspath += sourceSets.main.get().output + sourceSets.test.get().output
runtimeClasspath += sourceSets.main.get().output + sourceSets.test.get().output
srcDir("src/integration-test/java")
}
resources.srcDir("src/integration-test/resources")
}

configurations[integrationTest.implementationConfigurationName].extendsFrom(configurations.testImplementation.get())
configurations[integrationTest.runtimeOnlyConfigurationName].extendsFrom(configurations.testRuntimeOnly.get())

val integrationTestTask = tasks.register<Test>("integrationTest") {
group = "verification"

useJUnitPlatform()

testClassesDirs = integrationTest.output.classesDirs
classpath = sourceSets["integrationTest"].runtimeClasspath

shouldRunAfter("test")
}

tasks.check {
dependsOn(integrationTestTask)
}

--

--