Skip to content

Add Kotlin snippets for ten User Guide sections#5590

Open
yalishevant wants to merge 2 commits intojunit-team:mainfrom
yalishevant:kotlin-snippets-batch3
Open

Add Kotlin snippets for ten User Guide sections#5590
yalishevant wants to merge 2 commits intojunit-team:mainfrom
yalishevant:kotlin-snippets-batch3

Conversation

@yalishevant
Copy link
Copy Markdown
Contributor

@yalishevant yalishevant commented Apr 9, 2026

Adds Kotlin tabbed code snippets to ten more User Guide sections,
continuing the effort started in #5316 and #5432.

Pages updated (Writing Tests):

  • Built-in Extensions (@tempdir, @autoclose, Locale/TimeZone, SystemProperty)
  • Conditional Test Execution
  • Display Names
  • Annotations
  • Timeouts
  • Test Interfaces and Default Methods

Pages updated (Extensions):

  • Parameter Resolution
  • Keeping State in Extensions
  • Test Lifecycle Callbacks
  • Registering Extensions

I hereby agree to the terms of the JUnit Contributor License Agreement.


Definition of Done

  • There are no TODOs left in the code
  • Method preconditions are checked (N/A — documentation only)
  • Coding conventions have been followed
  • Change is covered by automated tests
  • Public API has Javadoc and @API annotations (N/A — documentation only)
  • Change is documented in the User Guide

Add tabbed Java/Kotlin code examples to ten documentation pages,
continuing the effort started in junit-team#5316 and junit-team#5432.

Pages updated (Writing Tests):
- Built-in Extensions (@tempdir, @autoclose, Locale/TimeZone, SystemProperty)
- Conditional Test Execution
- Display Names
- Annotations
- Timeouts
- Test Interfaces and Default Methods

Pages updated (Extensions):
- Parameter Resolution
- Keeping State in Extensions
- Test Lifecycle Callbacks
- Registering Extensions

Signed-off-by: Anton.Yalyshev <yalishev.ant@gmail.com>
@yalishevant yalishevant force-pushed the kotlin-snippets-batch3 branch from 8a1b05b to 6128746 Compare April 9, 2026 18:50
@marcphilipp marcphilipp added this to the 6.1.0-M2 milestone Apr 10, 2026
@marcphilipp marcphilipp self-requested a review April 10, 2026 06:56
Copy link
Copy Markdown
Member

@marcphilipp marcphilipp left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! 👍

Comment on lines +109 to +112
@Nested
// tag::default_timezone_class_level[]
@DefaultTimeZone("CET")
inner class MyTimeZoneTests {
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.

I guess there's no way to hide the inner keyword because of the formatter? I think we have two options: include @Nested as well or move it to a separate file. Thoughts?

Copy link
Copy Markdown
Contributor Author

@yalishevant yalishevant Apr 16, 2026

Choose a reason for hiding this comment

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

Went with including @Nested - moved the // tag:: markers before @Nested in both Java and Kotlin files so the annotation is now visible in the rendered snippets. This way the reader sees the full context without needing a separate file. IMO, some readers may find it useful. Let me know if you want to remove it from Java and keep only for Kotin.

Comment on lines +59 to +61
// tag::systemproperty_using_at_class_level[]
@ClearSystemProperty(key = "some property")
inner class MySystemPropertyTest {
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.

Ditto

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.

Done - same fix applied here

Comment on lines +85 to +88
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
// tag::systemproperty_class_restore_setup[]
@RestoreSystemProperties
inner class MySystemPropertyRestoreTest {
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 probably include the @TestInstance annotation here so one doesn't wonder why beforeAll() works. Same for the Java variant.

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.

Good point - moved the // tag:: markers before @TestInstance(PER_CLASS) in both Java and Kotlin so readers can see why beforeAll() works without a companion object.

Comment on lines +117 to +119
// tag::systemproperty_class_restore_isolated_class[]
@ReadsSystemProperty
inner class SomeOtherTestClass {
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.

Ditto

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.

--
[source,kotlin,indent=0]
----
include::example$kotlin/example/kotlin/DisplayNameDemo.kt[tags=user_guide]
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.

I wonder if we should a Kotlin-only example using backticks so spaced can be used in the function/test name directly. WDYT? 🤔

Copy link
Copy Markdown
Contributor Author

@yalishevant yalishevant Apr 16, 2026

Choose a reason for hiding this comment

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

Sure, great idea - added a TIP block after the @DisplayName example mentioning Kotlin's backtick-enclosed function names as an alternative for simple display names, with a short inline code sample.

Or you would rather wanted to see it as one more function inside DisplayNameDemo class? It will break symmetry but save space 🙂

Comment on lines +20 to +21
// tag::user_guide[]
@Tag("timeout")
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're not showing the tag in the Java snippet. The formatter enforces this ordering so I guess the only thing we can do to make it consistent is to include it in the Java version as well.

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.

Agreed - moved the // tag::user_guide[] marker before @Tag("timeout") in the Java file

}

companion object {
@JvmStatic
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.

There's not a strong reason for this method/function to be static. Let's make it non-static to simplify the Kotlin version.

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.

Right - removed the companion object wrapper and @JvmStatic.

earlier versions and your extension stores resources that need to be cleaned up, you
should implement both interfaces:

[source,java,indent=0]
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.

What about this one?

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.

Thanks! Added a Kotlin tab for the inline MyResource example in the Migration Note. Used a longer delimiter ====== for the outer TIP block to allow nesting the [tabs] block inside it.

Comment on lines +19 to +21
@RegisterExtension
val docs: DocumentationExtension =
DocumentationExtension.forPath(lookUpDocsDir())
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.

This needs @JvmField, doesn't it?

Suggested change
@RegisterExtension
val docs: DocumentationExtension =
DocumentationExtension.forPath(lookUpDocsDir())
@JvmField
@RegisterExtension
val docs =
DocumentationExtension.forPath(lookUpDocsDir())

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.

You're right! Added @JvmField before @RegisterExtension. Thank you

class TimingExtension :
BeforeTestExecutionCallback,
AfterTestExecutionCallback {
private val logger: Logger = Logger.getLogger(TimingExtension::class.java.name)
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.

Move into companion object?

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.

Right, it mirrors private static final semantics

@yalishevant yalishevant force-pushed the kotlin-snippets-batch3 branch from 529d18f to 6128746 Compare April 16, 2026 13:53
- Include @nested, @testinstance, @tag in tag scope
- Add Kotlin tab for inline MyResource snippet in TIP block
- Add @JvmField on @RegisterExtension instance field
- Move logger to companion object in TimingExtension
- Remove unnecessary companion object from sendRequest
- Add Kotlin-only TIP for backtick test names in display-names

Signed-off-by: Anton.Yalyshev <yalishev.ant@gmail.com>
@testlens-app
Copy link
Copy Markdown

testlens-app bot commented Apr 16, 2026

✅ All tests passed ✅

🏷️ Commit: 8b5d49b
▶️ Tests: 810 executed
⚪️ Checks: 15/15 completed


Learn more about TestLens at testlens.app.

@yalishevant yalishevant requested a review from marcphilipp April 16, 2026 14:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants