Skip to content

Commit c5bb451

Browse files
committed
Document support for Java records as test classes
Resolves #3892.
1 parent c734196 commit c5bb451

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

documentation/documentation.gradle.kts

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ plugins {
1313
alias(libs.plugins.gitPublish)
1414
alias(libs.plugins.plantuml)
1515
id("junitbuild.build-parameters")
16+
id("junitbuild.java-multi-release-test-sources")
1617
id("junitbuild.kotlin-library-conventions")
1718
id("junitbuild.testing-conventions")
1819
}
@@ -313,6 +314,13 @@ tasks {
313314
inputs.dir(kotlin.srcDirs.first())
314315
}
315316

317+
sourceSets["testRelease21"].apply {
318+
attributes(mapOf(
319+
"testRelease21Dir" to java.srcDirs.first()
320+
))
321+
inputs.dir(java.srcDirs.first())
322+
}
323+
316324
jvm {
317325
// To avoid warning, see https://github.com/asciidoctor/asciidoctor-gradle-plugin/issues/597
318326
jvmArgs(

documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
:testDir: ../../../../src/test/java
2+
:testRelease21Dir: ../../../../src/test/java21
3+
:kotlinTestDir: ../../../../src/test/kotlin
4+
15
[[writing-tests]]
26
== Writing Tests
37

@@ -120,6 +124,7 @@ Test Class::
120124
any top-level class, `static` member class, or <<writing-tests-nested,
121125
`@Nested` class>> that contains at least one _test method_, i.e. a _container_.
122126
Test classes must not be `abstract` and must have a single constructor.
127+
Java `record` classes are supported as well.
123128
124129
Test Method::
125130
any instance method that is directly annotated or meta-annotated with
@@ -178,6 +183,15 @@ lifecycle methods. For further information on runtime semantics, see
178183
include::{testDir}/example/StandardTests.java[tags=user_guide]
179184
----
180185

186+
It is also possible to use Java `record` classes as test classes as illustrated by the
187+
following example.
188+
189+
[source,java,indent=0]
190+
.A test class written as a Java record
191+
----
192+
include::{testRelease21Dir}/example/MyFirstJUnitJupiterRecordTests.java[tags=user_guide]
193+
----
194+
181195
[[writing-tests-display-names]]
182196
=== Display Names
183197

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package example;
12+
13+
// tag::user_guide[]
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
16+
import example.util.Calculator;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
record MyFirstJUnitJupiterRecordTests() {
21+
22+
@Test
23+
void addition() {
24+
assertEquals(2, new Calculator().add(1, 1));
25+
}
26+
27+
}
28+
// end::user_guide[]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import junitbuild.extensions.capitalized
2+
3+
plugins {
4+
id("junitbuild.java-library-conventions")
5+
}
6+
7+
listOf(21).forEach { javaVersion ->
8+
val sourceSet = sourceSets.register("testRelease${javaVersion}") {
9+
compileClasspath += sourceSets.main.get().output
10+
runtimeClasspath += sourceSets.main.get().output
11+
java {
12+
setSrcDirs(setOf("src/test/java${javaVersion}"))
13+
}
14+
}
15+
16+
configurations {
17+
named(sourceSet.get().compileClasspathConfigurationName).configure {
18+
extendsFrom(configurations.testCompileClasspath.get())
19+
}
20+
named(sourceSet.get().runtimeClasspathConfigurationName).configure {
21+
extendsFrom(configurations.testRuntimeClasspath.get())
22+
}
23+
}
24+
25+
tasks {
26+
val testTask = register<Test>("testRelease${javaVersion}") {
27+
group = "verification"
28+
description = "Runs the tests for Java ${javaVersion}"
29+
testClassesDirs = sourceSet.get().output.classesDirs
30+
classpath = sourceSet.get().runtimeClasspath
31+
}
32+
check {
33+
dependsOn(testTask)
34+
}
35+
named<JavaCompile>(sourceSet.get().compileJavaTaskName).configure {
36+
options.release = javaVersion
37+
}
38+
named<Checkstyle>("checkstyle${sourceSet.name.capitalized()}").configure {
39+
config = resources.text.fromFile(checkstyle.configDirectory.file("checkstyleTest.xml"))
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)