Skip to content

Commit

Permalink
extract common java test logic
Browse files Browse the repository at this point in the history
so we can re-use it also in the example-backend project

Signed-off-by: Kai Helbig <[email protected]>
  • Loading branch information
ostrya committed Jul 8, 2024
1 parent 3b1bcd4 commit 872b170
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 96 deletions.
79 changes: 45 additions & 34 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ allprojects {
ext {
assertj_version = '3.26.0'
jjwt_version = '0.12.6'
jsr305_version = '3.0.2'
junit4_version = '4.13.2'
junit5_version = '5.10.3'
keycloak_version = '25.0.1'
Expand All @@ -63,32 +64,64 @@ ext {
}

subprojects {
if (release_projects.contains(project.name)) {
apply plugin: 'jacoco'
if (release_projects.contains(project.name) || project.name == 'example-backend') {
if ('standalone' == project.name) {
apply plugin: 'java'
} else {
apply plugin: 'java-library'
}
apply plugin: 'maven-publish'
apply plugin: 'signing'
apply plugin: 'jvm-test-suite'
apply plugin: 'jacoco'

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
toolchain {
languageVersion = JavaLanguageVersion.of(8)
test {
useJUnitPlatform()
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(21)
}
testLogging {
events "passed", "skipped", "failed"
}
finalizedBy jacocoTestReport
}

tasks.named('compileTestJava') {
javaCompiler = javaToolchains.compilerFor {
languageVersion.set(JavaLanguageVersion.of(21))
}
}
tasks.withType(Test).configureEach {
javaLauncher = javaToolchains.launcherFor {
languageVersion = JavaLanguageVersion.of(21)

dependencies {
testImplementation platform("org.junit:junit-bom:$junit5_version")

testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'
}

jacocoTestReport {
reports {
xml.required = true
csv.required = false
html.required = false
}
}

sonar {
properties {
property "sonar.coverage.jacoco.xmlReportPaths", "${project.projectDir}/build/reports/jacoco/test/jacocoTestReport.xml"
}
}
}

if (release_projects.contains(project.name)) {
apply plugin: 'maven-publish'
apply plugin: 'signing'

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
toolchain {
languageVersion = JavaLanguageVersion.of(8)
}
}

Expand All @@ -114,10 +147,6 @@ subprojects {
}
}

dependencies {
implementation 'com.google.code.findbugs:jsr305:3.0.2'
}

publishing {
publications {
if ('standalone' != project.name) {
Expand Down Expand Up @@ -154,24 +183,6 @@ subprojects {
onlyIf { project.hasProperty('sonatypeUsername') }
}

test {
finalizedBy jacocoTestReport
}

jacocoTestReport {
reports {
xml.required = true
csv.required = false
html.required = false
}
}

sonar {
properties {
property "sonar.coverage.jacoco.xmlReportPaths", "${project.projectDir}/build/reports/jacoco/test/jacocoTestReport.xml"
}
}

afterEvaluate {
publishing.publications*.pom {
licenses {
Expand Down
20 changes: 3 additions & 17 deletions example-backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ buildscript {

apply plugin: 'com.google.cloud.tools.jib'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'jacoco'

ext['junit-jupiter.version'] = junit5_version

dependencies {
// Using multiple engines in a single project may lead to different JUnit
// Platform artifact versions being pulled in via transitive dependencies.
// Gradle allows you to enforce usage of fixed set of versions that match.
// https://docs.gradle.org/current/userguide/managing_transitive_dependencies.html#sec:bom_import
implementation(enforcedPlatform("org.junit:junit-bom:$junit5_version")) {
// https://docs.gradle.org/current/userguide/platforms.html#sub:bom_import
testImplementation(enforcedPlatform("org.junit:junit-bom:$junit5_version")) {
because 'enforce matching Platform, Jupiter, and Vintage versions'
}

Expand All @@ -39,20 +37,8 @@ dependencies {
testImplementation "io.rest-assured:json-path:$restassured_version"
testImplementation "io.rest-assured:xml-path:$restassured_version"
testImplementation "junit:junit:$junit4_version"
testImplementation "org.junit.jupiter:junit-jupiter:$junit5_version"
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation "org.assertj:assertj-core:$assertj_version"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit5_version"
testRuntimeOnly "org.junit.vintage:junit-vintage-engine:$junit5_version"
}

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
beforeTest { descriptor ->
logger.lifecycle("Running test: ${descriptor}")
}
}

jib {
Expand Down
10 changes: 1 addition & 9 deletions mock-junit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@ description = 'JUnit4 helper for keycloak-mock'

dependencies {
api project(':mock')
implementation "com.google.code.findbugs:jsr305:$jsr305_version"
implementation "junit:junit:$junit4_version"
testImplementation "io.rest-assured:rest-assured:$restassured_version"
testImplementation "org.assertj:assertj-core:$assertj_version"
}

test {
testLogging {
events "passed", "skipped", "failed"
}
beforeTest { descriptor ->
logger.lifecycle("Running test: ${descriptor}")
}
}
13 changes: 1 addition & 12 deletions mock-junit5/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@ description = 'JUnit5 helper for keycloak-mock'

dependencies {
api project(':mock')
implementation "com.google.code.findbugs:jsr305:$jsr305_version"
implementation "org.junit.jupiter:junit-jupiter-api:$junit5_version"
implementation "org.junit.jupiter:junit-jupiter-migrationsupport:$junit5_version"
testImplementation "io.rest-assured:rest-assured:$restassured_version"
testImplementation "org.assertj:assertj-core:$assertj_version"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit5_version"
}

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
beforeTest { descriptor ->
logger.lifecycle("Running test: ${descriptor}")
}
}
18 changes: 4 additions & 14 deletions mock/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ dependencies {
implementation "io.jsonwebtoken:jjwt-jackson:$jjwt_version"
implementation "io.vertx:vertx-web:$vertx_version"
implementation "io.vertx:vertx-web-templ-freemarker:$vertx_version"
implementation "org.slf4j:slf4j-api:$slf4j_version"
implementation "com.google.code.findbugs:jsr305:$jsr305_version"
implementation "com.google.dagger:dagger:$dagger_version"
implementation "org.slf4j:slf4j-api:$slf4j_version"
jsResourceJar "org.keycloak:keycloak-js-adapter:$keycloak_version@tar.gz"
htmlResourceJar "org.keycloak:keycloak-services:$keycloak_version"
testImplementation 'io.fusionauth:fusionauth-jwt:5.3.3'
Expand All @@ -28,10 +29,9 @@ dependencies {
testImplementation "io.vertx:vertx-codegen:$vertx_version"
testImplementation 'net.javacrumbs.json-unit:json-unit-assertj:3.4.0'
testImplementation "org.assertj:assertj-core:$assertj_version"
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit5_version"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junit5_version"
testImplementation "org.junit.jupiter:junit-jupiter-api"
testImplementation "org.junit.jupiter:junit-jupiter-params"
testImplementation "org.mockito:mockito-junit-jupiter:$mockito_version"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junit5_version"
testRuntimeOnly "org.slf4j:slf4j-simple:$slf4j_version"
annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"
}
Expand All @@ -53,13 +53,3 @@ tasks.register('addResources', Copy) {
}

processResources.dependsOn addResources

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
beforeTest { descriptor ->
logger.lifecycle("Running test: ${descriptor}")
}
}
11 changes: 1 addition & 10 deletions standalone/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ configurations {

dependencies {
implementation project(':mock')
implementation "com.google.code.findbugs:jsr305:$jsr305_version"
implementation "info.picocli:picocli:$picocli_version"
implementation "org.slf4j:slf4j-simple:$slf4j_version"
}
Expand All @@ -83,16 +84,6 @@ buildConfig {
buildConfigField('long', 'BUILD_TIME', "${System.currentTimeMillis()}L")
}

test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}
beforeTest { descriptor ->
logger.lifecycle("Running test: ${descriptor}")
}
}

jib {
from {
image = 'eclipse-temurin:21-jre'
Expand Down

0 comments on commit 872b170

Please sign in to comment.