Improve BeanFactoryExtensionsTests
Comparing to Java's `BeanFactory.getBean(Class<T> requiredType)`, Kotlin's `BeanFactory.getBean<T>()` supports generic type as `T`, this commit add test to cover that and add assertions. Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
This commit is contained in:
parent
10e32c92e6
commit
32647a1acf
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2023 the original author or authors.
|
* Copyright 2002-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -19,6 +19,7 @@ package org.springframework.beans.factory
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import io.mockk.verify
|
import io.mockk.verify
|
||||||
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.springframework.core.ResolvableType
|
import org.springframework.core.ResolvableType
|
||||||
|
|
||||||
|
@ -26,6 +27,7 @@ import org.springframework.core.ResolvableType
|
||||||
* Mock object based tests for BeanFactory Kotlin extensions.
|
* Mock object based tests for BeanFactory Kotlin extensions.
|
||||||
*
|
*
|
||||||
* @author Sebastien Deleuze
|
* @author Sebastien Deleuze
|
||||||
|
* @author Yanming Zhou
|
||||||
*/
|
*/
|
||||||
class BeanFactoryExtensionsTests {
|
class BeanFactoryExtensionsTests {
|
||||||
|
|
||||||
|
@ -35,10 +37,18 @@ class BeanFactoryExtensionsTests {
|
||||||
fun `getBean with reified type parameters`() {
|
fun `getBean with reified type parameters`() {
|
||||||
val foo = Foo()
|
val foo = Foo()
|
||||||
every { bf.getBeanProvider<Foo>(ofType<ResolvableType>()).getObject() } returns foo
|
every { bf.getBeanProvider<Foo>(ofType<ResolvableType>()).getObject() } returns foo
|
||||||
bf.getBean<Foo>()
|
assertThat(bf.getBean<Foo>()).isSameAs(foo)
|
||||||
verify { bf.getBeanProvider<ObjectProvider<Foo>>(ofType<ResolvableType>()).getObject() }
|
verify { bf.getBeanProvider<ObjectProvider<Foo>>(ofType<ResolvableType>()).getObject() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `getBean with reified generic type parameters`() {
|
||||||
|
val foo = listOf(Foo())
|
||||||
|
every { bf.getBeanProvider<List<Foo>>(ofType<ResolvableType>()).getObject() } returns foo
|
||||||
|
assertThat(bf.getBean<List<Foo>>()).isSameAs(foo)
|
||||||
|
verify { bf.getBeanProvider<ObjectProvider<List<Foo>>>(ofType<ResolvableType>()).getObject() }
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `getBean with String and reified type parameters`() {
|
fun `getBean with String and reified type parameters`() {
|
||||||
val name = "foo"
|
val name = "foo"
|
||||||
|
@ -52,7 +62,7 @@ class BeanFactoryExtensionsTests {
|
||||||
val arg2 = "arg2"
|
val arg2 = "arg2"
|
||||||
val bar = Bar(arg1, arg2)
|
val bar = Bar(arg1, arg2)
|
||||||
every { bf.getBeanProvider<Bar>(ofType<ResolvableType>()).getObject(arg1, arg2) } returns bar
|
every { bf.getBeanProvider<Bar>(ofType<ResolvableType>()).getObject(arg1, arg2) } returns bar
|
||||||
bf.getBean<Bar>(arg1, arg2)
|
assertThat(bf.getBean<Bar>(arg1, arg2)).isSameAs(bar)
|
||||||
verify { bf.getBeanProvider<Bar>(ofType<ResolvableType>()).getObject(arg1, arg2) }
|
verify { bf.getBeanProvider<Bar>(ofType<ResolvableType>()).getObject(arg1, arg2) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue