Skip to content

Emit Kotlin array literal for single-element @MockBean(types = ...)#1026

Merged
MBoegers merged 3 commits into
mainfrom
MBoegers/mockitobean-kotlin-single-element
May 26, 2026
Merged

Emit Kotlin array literal for single-element @MockBean(types = ...)#1026
MBoegers merged 3 commits into
mainfrom
MBoegers/mockitobean-kotlin-single-element

Conversation

@MBoegers
Copy link
Copy Markdown
Contributor

@MBoegers MBoegers commented May 26, 2026

Summary

UnwrapMockAndSpyBeanContainers previously emitted the Java single-element annotation-array shorthand (types = Foo::class) when a @MockBeans / @SpyBeans container collapsed to one inner annotation, regardless of source language. Kotlin rejects that shape:

Argument type mismatch: actual type is KClass<Foo>, but Array<KClass<*>> was expected.

The fix restricts the shorthand path to Java and lets the existing Kotlin K.ListLiteral branch handle size == 1. Java output is unchanged.

Before / after (Kotlin):

-@MockBean(types = Foo::class)
+@MockBean(types = [Foo::class])

Test plan

  • Updated kotlinSingleInnerAnnotation to assert the array-literal form — fails without the fix.
  • Added javaSingleInnerAnnotationKeepsShorthand confirming Java shorthand is preserved.
  • ./gradlew test --tests UnwrapMockAndSpyBeanContainersTest — 8/8 pass (3 Java + 5 Kotlin).

Scope note

This PR is intentionally narrow to the recipe that produced the reported failure; a broader sweep would be a separate piece of work.

MBoegers added 2 commits May 26, 2026 10:52
…yntax

The existing kotlinSingleInnerAnnotation test asserted the buggy
behaviour (Java single-element shorthand emitted for Kotlin). Update
it to assert the correct array-literal form, and add a Java sibling
that confirms Java keeps its shorthand. Without the fix the Kotlin
case fails:

  expected: @MockBean(types = [Foo::class])
  actual:   @MockBean(types = Foo::class)

Refs moderneinc/customer-requests#2435
When UnwrapMockAndSpyBeanContainers merges a one-element container
(e.g. @MockBeans(MockBean(Foo::class))) it previously emitted the
Java single-element shorthand `types = Foo::class` in both Java and
Kotlin output. Kotlin requires an array literal for Array<KClass<*>>
attributes, so the rewritten file did not compile.

Restrict the shorthand path to Java; let the Kotlin branch handle
size==1 by wrapping in K.ListLiteral, matching the existing N>=2
path.

Resolves moderneinc/customer-requests#2435
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite May 26, 2026
@MBoegers MBoegers marked this pull request as ready for review May 26, 2026 11:37
@MBoegers MBoegers requested a review from timtebeek May 26, 2026 11:37
@MBoegers MBoegers changed the title Emit Kotlin array literal for single-element @MockBean(types = ...) Emit Kotlin array literal for single-element @MockBean(types = ...) May 26, 2026
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite May 26, 2026
@MBoegers MBoegers merged commit 8e10d1e into main May 26, 2026
@MBoegers MBoegers deleted the MBoegers/mockitobean-kotlin-single-element branch May 26, 2026 11:56
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants