-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add Kotlin code examples to last writing tests topics #5898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danil-pavlov
wants to merge
7
commits into
junit-team:main
Choose a base branch
from
danil-pavlov:writing-tests-kotlin-part5
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
144793c
Add Kotlin code examples to last writing tests topics
danil-pavlov a029788
Fix Kotlin includes and examples
danil-pavlov 15ca983
Fix formatting
marcphilipp 3dc3554
Fix inner class output and record example
danil-pavlov 5f6f6ab
Fix Stream and consoleLauncherTest
danil-pavlov 6aa25af
Fix consoleLauncherTest with PER_CLASS
danil-pavlov 0c2db3e
Add notes for PER_CLASS
danil-pavlov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
916 changes: 902 additions & 14 deletions
916
...mentation/modules/ROOT/pages/writing-tests/parameterized-classes-and-tests.adoc
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
documentation/src/test/kotlin/example/kotlin/ExternalFieldSourceDemo.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| } |
29 changes: 29 additions & 0 deletions
29
documentation/src/test/kotlin/example/kotlin/ExternalMethodSourceDemo.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
| // 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[] | ||
55 changes: 55 additions & 0 deletions
55
documentation/src/test/kotlin/example/kotlin/MethodSourceParameterResolutionDemo.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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[] | ||
| } |
38 changes: 38 additions & 0 deletions
38
documentation/src/test/kotlin/example/kotlin/OrderedNestedTestClassesDemo.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
39
documentation/src/test/kotlin/example/kotlin/OrderedTestsDemo.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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