Skip to content

Commit

Permalink
Bruker jvm-test-suite for å konfigurere integrationTests task
Browse files Browse the repository at this point in the history
  • Loading branch information
henriwi committed Aug 15, 2024
1 parent b76dad8 commit f693db8
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ jobs:
distribution: 'temurin'
java-version: '21'

- name: Build with Gradle
run: ./gradlew build
- name: Run integration tests and build
run: ./gradlew integrationTest build
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
FROM gradle:8-jdk21-alpine AS build
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle shadowjar --no-daemon
RUN gradle integrationTest shadowjar --no-daemon

FROM eclipse-temurin:21-jdk-alpine
EXPOSE 8080:8080
RUN mkdir /app
COPY --from=build /home/gradle/src/build/libs/*.jar /app/matrikkel-bygning-egenregistrering.jar
ENTRYPOINT ["java", "-Dlogback.configurationFile=logback-cloud.xml", "-jar","/app/matrikkel-bygning-egenregistrering.jar"]
ENTRYPOINT ["java", "-Dlogback.configurationFile=logback-cloud.xml", "-jar","/app/matrikkel-bygning-egenregistrering.jar"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ nødvendig rundt miljøkonfigurasjon, det skal ha sane defaults.

### Integrasjonstester

Prosjektet inneholder noen integrasjonstester som ligger under [src/intTest](src/intTest). Testene bruker blant annet
Prosjektet inneholder noen integrasjonstester som ligger under [src/intTest](src/integrationTest). Testene bruker blant annet
testcontainers for å kjøre opp en database som bruke under testene.

Testene er definert med en egen task som kan kjøres slik:
Expand Down
59 changes: 30 additions & 29 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent.*

plugins {
kotlin("jvm").version(libs.versions.kotlinVersion)
kotlin("plugin.serialization").version(libs.versions.kotlinVersion)

alias(libs.plugins.ktor)
alias(libs.plugins.shadow)

`jvm-test-suite`
}

allprojects {
Expand All @@ -17,17 +21,6 @@ application {
mainClass.set("no.kartverket.matrikkel.bygning.ApplicationKt")
}

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

val intTestImplementation: Configuration by configurations.getting {
extendsFrom(configurations.implementation.get())
}

dependencies {
implementation(project(":matrikkel-api"))

Expand Down Expand Up @@ -59,36 +52,44 @@ dependencies {

// OpenAPI
implementation(libs.kompendium.core)

// Integration tests
intTestImplementation(libs.kotlin.test)
intTestImplementation(libs.ktor.server.tests)
intTestImplementation(libs.ktor.client.content.negotation)

intTestImplementation(libs.assertj)
intTestImplementation(libs.testcontainers.postgresql)
}

tasks {
named<ShadowJar>("shadowJar") {
mergeServiceFiles()
archiveBaseName.set("${project.name}-all")
}
}

check {
dependsOn(integrationTest)
tasks.withType<Test> {
testLogging {
showCauses = true
showExceptions = true
exceptionFormat = TestExceptionFormat.FULL
events(PASSED, SKIPPED, FAILED)
}
}

val integrationTest = task<Test>("integrationTest") {
description = "Runs integration tests"
group = "verification"
@Suppress("UnstableApiUsage")
testing {
suites {
withType<JvmTestSuite> {
useJUnitJupiter()
}

testClassesDirs = sourceSets["intTest"].output.classesDirs
classpath = sourceSets["intTest"].runtimeClasspath
shouldRunAfter("test")
register<JvmTestSuite>("integrationTest") {
dependencies {
implementation(project())

testLogging {
events("passed", "skipped", "failed")
implementation(libs.ktor.serialization.kotlinx)

implementation(libs.kotlin.test)
implementation(libs.ktor.server.tests)
implementation(libs.ktor.client.content.negotation)

implementation(libs.assertj)
implementation(libs.testcontainers.postgresql)
}
}
}
}
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ logstash-encoder = { group = "net.logstash.logback", name = "logstash-logback-en
logback-classic = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logbackVersion" }
jackson-databind = { group = "com.fasterxml.jackson.core", name = "jackson-databind", version.ref = "jacksonVersion" }
postgres = { group = "org.postgresql", name = "postgresql", version.ref = "postgresVersion" }
kotlin-test = { group = "org.jetbrains.kotlin", name = "kotlin-test-junit", version.ref = "kotlinVersion" }
kotlin-test = { group = "org.jetbrains.kotlin", name = "kotlin-test-junit5", version.ref = "kotlinVersion" }
kompendium-core = { group = "io.bkbn", name = "kompendium-core", version.ref = "kompendiumVersion" }
kotlinx-datetime = { group = "org.jetbrains.kotlinx", name = "kotlinx-datetime", version.ref = "kotlinxDatetimeVersion" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.ktor.client.request.*
import io.ktor.http.*
import io.ktor.server.testing.*
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.jupiter.api.Test

class InternalRouteTest : TestWithDb() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ import io.ktor.client.plugins.contentnegotiation.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.config.*
import io.ktor.server.testing.*
import org.junit.AfterClass
import org.junit.BeforeClass
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.testcontainers.containers.PostgreSQLContainer

abstract class TestWithDb {

companion object {
private val postgresSQLContainer = PostgreSQLContainer("postgres:15-alpine")

@BeforeClass
@BeforeAll
@JvmStatic
fun setUp() {
postgresSQLContainer.withDatabaseName("bygning")
postgresSQLContainer.start()
}

@AfterClass
@AfterAll
@JvmStatic
fun tearDown() {
postgresSQLContainer.stop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.ktor.server.testing.*
import no.kartverket.matrikkel.bygning.TestWithDb
import no.kartverket.matrikkel.bygning.models.Bygning
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.jupiter.api.Test

class BygningRouteTest : TestWithDb() {

Expand Down

0 comments on commit f693db8

Please sign in to comment.