Conversation
Applying the recipe to Kotlin test sources threw
"Expected a template that would generate exactly one statement to
replace one statement, but generated 0" because Java statement
templates produce no statements on a Kotlin LST.
Branch on K.CompilationUnit at each template site:
- private lateinit var mocks: AutoCloseable instead of a Java field
- @AfterEach fun tearDown() {} without throws (no checked exceptions)
- mocks = MockitoAnnotations.openMocks(this) statement replacement
- mocks.close() appended to the @AfterEach body
KotlinTemplate does not support contextSensitive(), so references to
the mocks field parse untyped; type attribution is patched onto the
identifiers and the close() invocation after each template apply.
Class-wide autoformat is skipped for Kotlin sources since it would
reformat unrelated code across the whole class.
Fixes moderneinc/customer-requests#2825
timtebeek
approved these changes
Jul 21, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
ReplaceInitMockToOpenMockappliedJavaTemplates unconditionally, which can only emit Java, so it crashed with "Expected a template that would generate exactly one statement to replace one statement, but generated 0" whenMockitoAnnotations.initMocks(this)appeared in a Kotlin@BeforeEachfunction.Each of the recipe's four template sites now branches on
K.CompilationUnitand uses aKotlinTemplatefor Kotlin sources:private lateinit var mocks: AutoCloseablefor the generated field@AfterEach fun tearDown() {}withoutthrows— Kotlin has no checked exceptions, so theaddThrowsIfAbsentlogic stays Java-onlymocks = MockitoAnnotations.openMocks(this)for the statement replacementmocks.close()appended to the@AfterEachbodyTwo Kotlin-specific notes.
KotlinTemplatedoes not supportcontextSensitive()(it throwsUnsupportedOperationException), so references to the generatedmocksfield come back untyped from the context-free parse; they are type-attributed explicitly after each template apply, which keeps fullTypeValidationon in the tests. Second, class-wideautoFormatis skipped for Kotlin sources since it reformats unrelated code across the entire class — verified against a real-world 800-line Kotlin test file, where the recipe's diff is now exactly the four intended changes.Java behavior is unchanged. New Kotlin tests cover the base transformation and appending
close()to a pre-existing@AfterEachfunction.