@@ -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
145160In 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
173204The 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
192236The 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
222294TIP: 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
251338In 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
276378The 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
295410The 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
325468TIP: The spies can also be injected into `@Configuration` classes or other test-related
0 commit comments