Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,26 @@ used to filter xref:running-tests/intro.adoc[test discovery and execution]. Plea
xref:running-tests/tags.adoc[] section for more information about tag support in the JUnit
Platform.

[tabs]
====
Java::
+
--
[source,java,indent=0]
----
include::example$java/example/TaggingDemo.java[tags=user_guide]
----
--

Kotlin::
+
--
[source,kotlin,indent=0]
----
include::example$kotlin/example/kotlin/TaggingDemo.kt[tags=user_guide]
----
--
====

TIP: See xref:writing-tests/annotations.adoc#annotations[Meta-Annotations and Composed Annotations] for examples demonstrating how to create
custom annotations for tags.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,26 @@ NOTE: See also: xref:extensions/relative-execution-order-of-user-code-and-extens
The following example demonstrates how to guarantee that test methods are executed in the
order specified via the `@Order` annotation.

[tabs]
====
Java::
+
--
[source,java,indent=0]
----
include::example$java/example/OrderedTestsDemo.java[tags=user_guide]
----
--

Kotlin::
+
--
[source,kotlin,indent=0]
----
include::example$kotlin/example/kotlin/OrderedTestsDemo.kt[tags=user_guide]
----
--
====

[[methods-default]]
=== Setting the Default Method Orderer
Expand Down Expand Up @@ -132,7 +148,23 @@ Note that a local `@TestClassOrder` declaration always overrides an inherited
The following example demonstrates how to guarantee that `@Nested` test classes are
executed in the order specified via the `@Order` annotation.

[tabs]
====
Java::
+
--
[source,java,indent=0]
----
include::example$java/example/OrderedNestedTestClassesDemo.java[tags=user_guide]
----
--

Kotlin::
+
--
[source,kotlin,indent=0]
----
include::example$kotlin/example/kotlin/OrderedNestedTestClassesDemo.kt[tags=user_guide]
----
--
====
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2015-2026 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package example.kotlin

import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.FieldSource

class ExternalFieldSourceDemo {
// tag::external_field_FieldSource_example[]
@ParameterizedTest
@FieldSource("example.kotlin.FruitUtils#tropicalFruits")
fun testWithExternalFieldSource(tropicalFruit: String) {
// test with tropicalFruit
}
// end::external_field_FieldSource_example[]
}

object FruitUtils {
@JvmField
val tropicalFruits: List<String> = listOf("pineapple", "kiwi")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2015-2026 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package example.kotlin

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should include the package declaration in the rendered snippet because it's referenced below. If that doesn't work due to the formatter, we can duplicate it in the adoc file.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I had it originally (24490a6), I guess it's the formatter problem


// tag::external_MethodSource_example[]
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource

class ExternalMethodSourceDemo {
@ParameterizedTest
@MethodSource("example.kotlin.StringsProviders#tinyStrings")
fun testWithExternalMethodSource(tinyString: String) {
// test with tiny string
}
}

object StringsProviders {
@JvmStatic
fun tinyStrings() = sequenceOf(".", "oo", "OOO")
}
// end::external_MethodSource_example[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2015-2026 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package example.kotlin

import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.api.extension.ParameterContext
import org.junit.jupiter.api.extension.ParameterResolver
import org.junit.jupiter.api.extension.RegisterExtension
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments.arguments
import org.junit.jupiter.params.provider.MethodSource

@TestInstance(PER_CLASS)
class MethodSourceParameterResolutionDemo {
// tag::parameter_resolution_MethodSource_example[]
@JvmField
@RegisterExtension
val integerResolver = IntegerResolver()

@ParameterizedTest
@MethodSource("factoryMethodWithArguments")
fun testWithFactoryMethodWithArguments(argument: String) {
assertTrue(argument.startsWith("2"))
}

fun factoryMethodWithArguments(quantity: Int) =
sequenceOf(
arguments("$quantity apples"),
arguments("$quantity lemons")
)

class IntegerResolver : ParameterResolver {
override fun supportsParameter(
parameterContext: ParameterContext,
extensionContext: ExtensionContext
): Boolean = parameterContext.parameter.type == Int::class.javaPrimitiveType

override fun resolveParameter(
parameterContext: ParameterContext,
extensionContext: ExtensionContext
): Any = 2
}
// end::parameter_resolution_MethodSource_example[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2015-2026 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package example.kotlin

// tag::user_guide[]
import org.junit.jupiter.api.ClassOrderer
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Order
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestClassOrder

@TestClassOrder(ClassOrderer.OrderAnnotation::class)
class OrderedNestedTestClassesDemo {
@Nested
@Order(1)
inner class PrimaryTests {
@Test
fun test1() {
}
}

@Nested
@Order(2)
inner class SecondaryTests {
@Test
fun test2() {
}
}
}
// end::user_guide[]
39 changes: 39 additions & 0 deletions documentation/src/test/kotlin/example/kotlin/OrderedTestsDemo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2015-2026 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/

package example.kotlin

// tag::user_guide[]
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation
import org.junit.jupiter.api.Order
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestMethodOrder

@TestMethodOrder(OrderAnnotation::class)
class OrderedTestsDemo {
@Test
@Order(1)
fun nullValues() {
// perform assertions against null values
}

@Test
@Order(2)
fun emptyValues() {
// perform assertions against empty values
}

@Test
@Order(3)
fun validValues() {
// perform assertions against valid values
}
}
// end::user_guide[]
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,114 @@

package example.kotlin

import example.util.StringUtils
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.parallel.Execution
import org.junit.jupiter.api.parallel.ExecutionMode.SAME_THREAD
import org.junit.jupiter.params.Parameter
import org.junit.jupiter.params.ParameterizedClass
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import org.junit.jupiter.params.provider.ValueSource
import java.time.Duration

class ParameterizedClassDemo {
// @formatter:off
@Nested
// tag::first_example[]
@ParameterizedClass
@ValueSource(strings = ["racecar", "radar", "able was I ere I saw elba"])
// end::first_example[]
inner
// tag::first_example[]
class PalindromeTests {
// end::first_example[]
// @formatter:on
// tag::first_example[]
@Parameter
lateinit var candidate: String

@Test
fun palindrome() {
assertTrue(StringUtils.isPalindrome(candidate))
}

@Test
fun reversePalindrome() {
val reverseCandidate = candidate.reversed()
assertTrue(StringUtils.isPalindrome(reverseCandidate))
}
}
// end::first_example[]

@Nested
inner class ConstructorInjection {
// @formatter:off
@Nested
// tag::constructor_injection[]
@ParameterizedClass
@CsvSource("apple, 23", "banana, 42")
// end::constructor_injection[]
inner
// tag::constructor_injection[]
class FruitTests(
private val fruit: String,
private val quantity: Int
) {
// end::constructor_injection[]
// @formatter:on
// tag::constructor_injection[]
@Test
fun test() {
assertFruit(fruit)
assertQuantity(quantity)
}

@Test
fun anotherTest() {
// ...
}
}
// end::constructor_injection[]
}

@Nested
inner class FieldInjection {
// @formatter:off
@Nested
// tag::field_injection[]
@ParameterizedClass
@CsvSource("apple, 23", "banana, 42")
// end::field_injection[]
inner
// tag::field_injection[]
class FruitTests {
// end::field_injection[]
// @formatter:on
// tag::field_injection[]
@Parameter(0)
lateinit var fruit: String

@Parameter(1)
var quantity: Int = 0

@Test
fun test() {
assertFruit(fruit)
assertQuantity(quantity)
}

@Test
fun anotherTest() {
// ...
}
}
// end::field_injection[]
}

// @formatter:off
@Nested
// tag::nested[]
Expand Down Expand Up @@ -58,7 +154,7 @@ class ParameterizedClassDemo {

private fun assertFruit(fruit: String) {
assertTrue(
listOf("apple", "banana", "cherry", "dewberry").contains(fruit)
fruit in listOf("apple", "banana", "cherry", "dewberry")
) { "not a fruit: $fruit" }
}

Expand Down
Loading
Loading