Skip to content

Commit 6d3d3d1

Browse files
committed
Introduce Kotlin examples for Bean Overrides (@⁠MockitoBean, etc.)
This commit introduces Kotlin examples in the reference manual for @⁠MockitoBean, @⁠MockitoSpyBean, and @⁠TestBean. Closes gh-36541
1 parent 9140ed6 commit 6d3d3d1

2 files changed

Lines changed: 186 additions & 0 deletions

File tree

framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-mockitobean.adoc

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,21 @@ Java::
140140
}
141141
----
142142
<1> Replace the bean with type `CustomService` with a Mockito mock.
143+
144+
Kotlin::
145+
+
146+
[source,kotlin,indent=0,subs="verbatim,quotes"]
147+
----
148+
@SpringJUnitConfig(TestConfig::class)
149+
class BeanOverrideTests {
150+
151+
@MockitoBean // <1>
152+
lateinit var customService: CustomService
153+
154+
// tests...
155+
}
156+
----
157+
<1> Replace the bean with type `CustomService` with a Mockito mock.
143158
======
144159

145160
In the example above, we are creating a mock for `CustomService`. If more than one bean
@@ -168,6 +183,22 @@ Java::
168183
}
169184
----
170185
<1> Replace the bean named `service` with a Mockito mock.
186+
187+
Kotlin::
188+
+
189+
[source,kotlin,indent=0,subs="verbatim,quotes"]
190+
----
191+
@SpringJUnitConfig(TestConfig::class)
192+
class BeanOverrideTests {
193+
194+
@MockitoBean("service") // <1>
195+
lateinit var customService: CustomService
196+
197+
// tests...
198+
199+
}
200+
----
201+
<1> Replace the bean named `service` with a Mockito mock.
171202
======
172203

173204
The following `@SharedMocks` annotation registers two mocks by-type and one mock by-name.
@@ -187,6 +218,19 @@ Java::
187218
----
188219
<1> Register `OrderService` and `UserService` mocks by-type.
189220
<2> Register `PrintingService` mock by-name.
221+
222+
Kotlin::
223+
+
224+
[source,kotlin,indent=0,subs="verbatim,quotes"]
225+
----
226+
@Target(AnnotationTarget.CLASS)
227+
@Retention(AnnotationRetention.RUNTIME)
228+
@MockitoBean(types = [OrderService::class, UserService::class]) // <1>
229+
@MockitoBean(name = "ps1", types = [PrintingService::class]) // <2>
230+
annotation class SharedMocks
231+
----
232+
<1> Register `OrderService` and `UserService` mocks by-type.
233+
<2> Register `PrintingService` mock by-name.
190234
======
191235

192236
The following demonstrates how `@SharedMocks` can be used on a test class.
@@ -217,6 +261,34 @@ Java::
217261
----
218262
<1> Register common mocks via the custom `@SharedMocks` annotation.
219263
<2> Optionally inject mocks to _stub_ or _verify_ them.
264+
265+
Kotlin::
266+
+
267+
[source,kotlin,indent=0,subs="verbatim,quotes"]
268+
----
269+
@SpringJUnitConfig(TestConfig::class)
270+
@SharedMocks // <1>
271+
class BeanOverrideTests {
272+
273+
@Autowired
274+
lateinit var orderService: OrderService // <2>
275+
276+
@Autowired
277+
lateinit var userService: UserService // <2>
278+
279+
@Autowired
280+
lateinit var ps1: PrintingService // <2>
281+
282+
// Inject other components that rely on the mocks.
283+
284+
@Test
285+
fun testThatDependsOnMocks() {
286+
// ...
287+
}
288+
}
289+
----
290+
<1> Register common mocks via the custom `@SharedMocks` annotation.
291+
<2> Optionally inject mocks to _stub_ or _verify_ them.
220292
======
221293

222294
TIP: The mocks can also be injected into `@Configuration` classes or other test-related
@@ -246,6 +318,21 @@ Java::
246318
}
247319
----
248320
<1> Wrap the bean with type `CustomService` with a Mockito spy.
321+
322+
Kotlin::
323+
+
324+
[source,kotlin,indent=0,subs="verbatim,quotes"]
325+
----
326+
@SpringJUnitConfig(TestConfig::class)
327+
class BeanOverrideTests {
328+
329+
@MockitoSpyBean // <1>
330+
lateinit var customService: CustomService
331+
332+
// tests...
333+
}
334+
----
335+
<1> Wrap the bean with type `CustomService` with a Mockito spy.
249336
======
250337

251338
In the example above, we are wrapping the bean with type `CustomService`. If more than
@@ -271,6 +358,21 @@ Java::
271358
}
272359
----
273360
<1> Wrap the bean named `service` with a Mockito spy.
361+
362+
Kotlin::
363+
+
364+
[source,kotlin,indent=0,subs="verbatim,quotes"]
365+
----
366+
@SpringJUnitConfig(TestConfig::class)
367+
class BeanOverrideTests {
368+
369+
@MockitoSpyBean("service") // <1>
370+
lateinit var customService: CustomService
371+
372+
// tests...
373+
}
374+
----
375+
<1> Wrap the bean named `service` with a Mockito spy.
274376
======
275377

276378
The following `@SharedSpies` annotation registers two spies by-type and one spy by-name.
@@ -290,6 +392,19 @@ Java::
290392
----
291393
<1> Register `OrderService` and `UserService` spies by-type.
292394
<2> Register `PrintingService` spy by-name.
395+
396+
Kotlin::
397+
+
398+
[source,kotlin,indent=0,subs="verbatim,quotes"]
399+
----
400+
@Target(AnnotationTarget.CLASS)
401+
@Retention(AnnotationRetention.RUNTIME)
402+
@MockitoSpyBean(types = [OrderService::class, UserService::class]) // <1>
403+
@MockitoSpyBean(name = "ps1", types = [PrintingService::class]) // <2>
404+
annotation class SharedSpies
405+
----
406+
<1> Register `OrderService` and `UserService` spies by-type.
407+
<2> Register `PrintingService` spy by-name.
293408
======
294409

295410
The following demonstrates how `@SharedSpies` can be used on a test class.
@@ -320,6 +435,34 @@ Java::
320435
----
321436
<1> Register common spies via the custom `@SharedSpies` annotation.
322437
<2> Optionally inject spies to _stub_ or _verify_ them.
438+
439+
Kotlin::
440+
+
441+
[source,kotlin,indent=0,subs="verbatim,quotes"]
442+
----
443+
@SpringJUnitConfig(TestConfig::class)
444+
@SharedSpies // <1>
445+
class BeanOverrideTests {
446+
447+
@Autowired
448+
lateinit var orderService: OrderService // <2>
449+
450+
@Autowired
451+
lateinit var userService: UserService // <2>
452+
453+
@Autowired
454+
lateinit var ps1: PrintingService // <2>
455+
456+
// Inject other components that rely on the spies.
457+
458+
@Test
459+
fun testThatDependsOnMocks() {
460+
// ...
461+
}
462+
}
463+
----
464+
<1> Register common spies via the custom `@SharedSpies` annotation.
465+
<2> Optionally inject spies to _stub_ or _verify_ them.
323466
======
324467

325468
TIP: The spies can also be injected into `@Configuration` classes or other test-related

framework-docs/modules/ROOT/pages/testing/annotations/integration-spring/annotation-testbean.adoc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,27 @@ Java::
7373
----
7474
<1> Mark a field for overriding the bean with type `CustomService`.
7575
<2> The result of this static method will be used as the instance and injected into the field.
76+
77+
Kotlin::
78+
+
79+
[source,kotlin,indent=0,subs="verbatim,quotes"]
80+
----
81+
class OverrideBeanTests {
82+
@TestBean // <1>
83+
lateinit var customService: CustomService
84+
85+
// test case body...
86+
87+
companion object {
88+
@JvmStatic
89+
fun customService(): CustomService { // <2>
90+
return MyFakeCustomService()
91+
}
92+
}
93+
}
94+
----
95+
<1> Mark a field for overriding the bean with type `CustomService`.
96+
<2> The result of this static method will be used as the instance and injected into the field.
7697
======
7798

7899
In the example above, we are overriding the bean with type `CustomService`. If more than
@@ -102,6 +123,28 @@ Java::
102123
<1> Mark a field for overriding the bean with name `service`, and specify that the
103124
factory method is named `createCustomService`.
104125
<2> The result of this static method will be used as the instance and injected into the field.
126+
127+
Kotlin::
128+
+
129+
[source,kotlin,indent=0,subs="verbatim,quotes"]
130+
----
131+
class OverrideBeanTests {
132+
@TestBean(name = "service", methodName = "createCustomService") // <1>
133+
lateinit var customService: CustomService
134+
135+
// test case body...
136+
137+
companion object {
138+
@JvmStatic
139+
fun createCustomService(): CustomService { // <2>
140+
return MyFakeCustomService()
141+
}
142+
}
143+
}
144+
----
145+
<1> Mark a field for overriding the bean with name `service`, and specify that the
146+
factory method is named `createCustomService`.
147+
<2> The result of this static method will be used as the instance and injected into the field.
105148
======
106149

107150
[TIP]

0 commit comments

Comments
 (0)