Skip to content

Commit

Permalink
run tests with JUnit Platform
Browse files Browse the repository at this point in the history
To pave the way to migrate to JUnit 5 we now switch the build to use the JUnit Platform runner
by switching to `useJUnitPlatform {..}` in our Gradle build files.
This doesn't effectively change much on the surface, because we still keep all JUnit 4 annotations and test infrastructure.
But it now runs our JUnit 4 tests using the JUnit Vintage Engine via the Gradle JUnit Platform integration,
instead of directly through the Gradle JUnit 4 integration.
This effectively allows us to combine JUnit 4 and JUnit 5 tests in the same Gradle module
since we can combine any number of JUnit Platform test engines.
We include both JUnit Vintage and JUnit Jupiter engine, so it will pick up JUnit 4 tests as well as JUnit 5 tests.
Then we can migrate tests to JUnit 5 at any pace and any granularity that we want.

Note that within `archunit-integration-test` the situation is a little tricky,
because we include both `archunit-junit4` and `archunit-junit5`.
And those share some annotations, but with different effect.
For example, `@AnalyzeClasses` alone triggers the `archunit-junit5-engine` to run a test class.
If it's also annotated with `@RunWith(ArchUnitRunner.class)`, then the JUnit Vintage Engine will pick it up
and run it as a JUnit 4 test.
Thus, I noticed that the tests also annotated with `@RunWith(ArchUnitRunner.class)` are run twice there.
Since this is an irrelevant corner case for real life scenarios, I've simply removed the `@RunWith`,
because we want to run our tests with `archunit-junit5` anyway.

I've compared the Gradle test xml report of all the executed tests before and after.
From what I see the output coincides for both standard and slow tests
(except for one wrongfully executed slow test in `archunit-junit` before the change,
but this is then no problem but instead fixed now).

Signed-off-by: Peter Gafert <[email protected]>
  • Loading branch information
codecholeric committed Jun 9, 2024
1 parent 6643b9e commit c649f41
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 50 deletions.
6 changes: 3 additions & 3 deletions archunit-example/example-plain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ dependencies {
}

test {
if (!project.hasProperty('example')) {
useJUnit {
excludeCategories 'com.tngtech.archunit.exampletest.Example'
useJUnitPlatform {
if (!project.hasProperty('example')) {
excludeTags('com.tngtech.archunit.exampletest.Example')
}
}
}
4 changes: 0 additions & 4 deletions archunit-integration-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,3 @@ dependencies {

testRuntimeOnly project(path: ':archunit-junit5-engine')
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.tngtech.archunit.junit.ArchTests;
import com.tngtech.archunit.junit.ArchUnitRunner;
import com.tngtech.archunit.lang.ArchRule;
import org.junit.runner.RunWith;

import static com.tngtech.archunit.base.DescribedPredicate.not;
import static com.tngtech.archunit.core.domain.JavaAccess.Predicates.origin;
Expand All @@ -35,7 +34,6 @@
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.library.Architectures.layeredArchitecture;

@RunWith(ArchUnitRunner.class)
@AnalyzeClasses(
packagesOf = ArchUnitArchitectureTest.class,
importOptions = ArchUnitArchitectureTest.ArchUnitProductionCode.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
import com.tngtech.archunit.core.importer.Location;
import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.junit.ArchUnitRunner;
import com.tngtech.archunit.lang.ArchRule;
import org.junit.runner.RunWith;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;

@RunWith(ArchUnitRunner.class)
@AnalyzeClasses(packages = "com.tngtech.archunit", importOptions = ArchUnitExampleLocations.class)
public class ArchUnitExampleArchitectureTest {
@ArchTest
Expand Down
9 changes: 0 additions & 9 deletions archunit-java-modules-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ ext.minimumJavaVersion = JavaVersion.VERSION_1_9
dependencies {
testImplementation project(path: ':archunit', configuration: 'shadow')
testImplementation dependency.log4j_slf4j

testImplementation dependency.junit5JupiterApi

testRuntimeOnly dependency.junitPlatform
testRuntimeOnly dependency.junit5JupiterEngine
}

test {
useJUnitPlatform()
}

def addArchUnitModuleOptions = {
Expand Down
3 changes: 0 additions & 3 deletions archunit-junit/junit5/engine-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ dependencies {
testImplementation project(path: ':archunit', configuration: 'tests')
testImplementation dependency.assertj
testImplementation dependency.mockito
testImplementation dependency.junit5JupiterApi

testRuntimeOnly dependency.junit5JupiterEngine
}

compileJava.dependsOn project(':archunit-junit5-api').finishArchive
Expand Down
5 changes: 1 addition & 4 deletions archunit-junit/junit5/engine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ dependencies {
testImplementation dependency.assertj
testImplementation dependency.mockito
testImplementation dependency.mockito_junit5
testImplementation dependency.junit5JupiterApi
testImplementation dependency.log4j_core
testImplementation dependency.log4j_slf4j

testRuntimeOnly dependency.junit5JupiterEngine
}

gradle.projectsEvaluated {
Expand All @@ -38,7 +35,7 @@ sourcesJar {
}

test {
useJUnitPlatform() {
useJUnitPlatform {
excludeEngines 'archunit'
}
}
Expand Down
3 changes: 2 additions & 1 deletion archunit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ publishing {

archUnitTest {
providesTestJar = true
hasSlowTests = true
}

def jdk9MainDirs = ['src/jdk9main/java']
Expand All @@ -68,12 +67,14 @@ sourceSets {
srcDirs = jdk9TestDirs
}
compileClasspath += sourceSets.test.compileClasspath + sourceSets.jdk9main.output.classesDirs
runtimeClasspath += sourceSets.test.runtimeClasspath
}
jdk16test {
java {
srcDirs = jdk16TestDirs
}
compileClasspath += sourceSets.test.compileClasspath + sourceSets.jdk9main.output.classesDirs
runtimeClasspath += sourceSets.test.runtimeClasspath
}
}

Expand Down
47 changes: 26 additions & 21 deletions buildSrc/src/main/groovy/archunit.java-testing-conventions.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
plugins {
id 'java-library'
}

abstract class ArchUnitTestExtension {
boolean providesTestJar = false
boolean hasSlowTests = false
}

ext.archUnitTest = extensions.create('archUnitTest', ArchUnitTestExtension)

dependencies {
testImplementation dependency.junit5JupiterApi

testRuntimeOnly dependency.junit5JupiterEngine
testRuntimeOnly dependency.junit5VintageEngine
}

tasks.withType(Test) {
maxHeapSize = "2G"

useJUnitPlatform {
if (!project.hasProperty('allTests')) {
excludeTags('com.tngtech.archunit.Slow')
}
}

testLogging {
events "failed"
exceptionFormat "full"
}
}

afterEvaluate {
if (archUnitTest.providesTestJar) {
configurations {
Expand All @@ -20,26 +45,6 @@ afterEvaluate {
tests testJar
}
}

if (archUnitTest.hasSlowTests) {
tasks.withType(Test) {
assert !it.testFramework.class.name.contains('JUnitPlatform'): "Slow tests are not yet supported for JUnit 5 tests"
if (!project.hasProperty('allTests')) {
useJUnit {
excludeCategories 'com.tngtech.archunit.Slow'
}
}
}
}
}

tasks.withType(Test) {
maxHeapSize = "2G"

testLogging {
events "failed"
exceptionFormat "full"
}
}

// Add some better test failure reporting on the console
Expand Down

0 comments on commit c649f41

Please sign in to comment.