Add Kotlin snippets for ten User Guide sections#5590
Add Kotlin snippets for ten User Guide sections#5590yalishevant wants to merge 2 commits intojunit-team:mainfrom
Conversation
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>
8a1b05b to
6128746
Compare
| @Nested | ||
| // tag::default_timezone_class_level[] | ||
| @DefaultTimeZone("CET") | ||
| inner class MyTimeZoneTests { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| // tag::systemproperty_using_at_class_level[] | ||
| @ClearSystemProperty(key = "some property") | ||
| inner class MySystemPropertyTest { |
There was a problem hiding this comment.
Done - same fix applied here
| @TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
| // tag::systemproperty_class_restore_setup[] | ||
| @RestoreSystemProperties | ||
| inner class MySystemPropertyRestoreTest { |
There was a problem hiding this comment.
We should probably include the @TestInstance annotation here so one doesn't wonder why beforeAll() works. Same for the Java variant.
There was a problem hiding this comment.
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.
| // tag::systemproperty_class_restore_isolated_class[] | ||
| @ReadsSystemProperty | ||
| inner class SomeOtherTestClass { |
| -- | ||
| [source,kotlin,indent=0] | ||
| ---- | ||
| include::example$kotlin/example/kotlin/DisplayNameDemo.kt[tags=user_guide] |
There was a problem hiding this comment.
I wonder if we should a Kotlin-only example using backticks so spaced can be used in the function/test name directly. WDYT? 🤔
There was a problem hiding this comment.
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 🙂
| // tag::user_guide[] | ||
| @Tag("timeout") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Agreed - moved the // tag::user_guide[] marker before @Tag("timeout") in the Java file
| } | ||
|
|
||
| companion object { | ||
| @JvmStatic |
There was a problem hiding this comment.
There's not a strong reason for this method/function to be static. Let's make it non-static to simplify the Kotlin version.
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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.
| @RegisterExtension | ||
| val docs: DocumentationExtension = | ||
| DocumentationExtension.forPath(lookUpDocsDir()) |
There was a problem hiding this comment.
This needs @JvmField, doesn't it?
| @RegisterExtension | |
| val docs: DocumentationExtension = | |
| DocumentationExtension.forPath(lookUpDocsDir()) | |
| @JvmField | |
| @RegisterExtension | |
| val docs = | |
| DocumentationExtension.forPath(lookUpDocsDir()) |
There was a problem hiding this comment.
You're right! Added @JvmField before @RegisterExtension. Thank you
| class TimingExtension : | ||
| BeforeTestExecutionCallback, | ||
| AfterTestExecutionCallback { | ||
| private val logger: Logger = Logger.getLogger(TimingExtension::class.java.name) |
There was a problem hiding this comment.
Move into companion object?
There was a problem hiding this comment.
Right, it mirrors private static final semantics
529d18f to
6128746
Compare
- 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>
✅ All tests passed ✅🏷️ Commit: 8b5d49b Learn more about TestLens at testlens.app. |
Adds Kotlin tabbed code snippets to ten more User Guide sections,
continuing the effort started in #5316 and #5432.
Pages updated (Writing Tests):
Pages updated (Extensions):
I hereby agree to the terms of the JUnit Contributor License Agreement.
Definition of Done
Method preconditions are checked(N/A — documentation only)Public API has Javadoc and(N/A — documentation only)@APIannotations