Add comprehensive tests of Kotlin extensions
This commit also removes extensions hidden by Java API (varargs).
This commit is contained in:
@@ -19,16 +19,6 @@ fun <T : Any> BeanFactory.getBean(requiredType: KClass<T>): T = getBean(required
|
||||
*/
|
||||
inline fun <reified T : Any> BeanFactory.getBean(): T = getBean(T::class.java)
|
||||
|
||||
/**
|
||||
* Extension for [BeanFactory.getBean] providing a [KClass] based variant.
|
||||
*
|
||||
* @see BeanFactory.getBean(String, Class<T>)
|
||||
* @author Sebastien Deleuze
|
||||
* @since 5.0
|
||||
*/
|
||||
fun <T : Any> BeanFactory.getBean(name: String, requiredType: KClass<T>): T =
|
||||
getBean(name, requiredType.java)
|
||||
|
||||
/**
|
||||
* Extension for [BeanFactory.getBean] providing a `getBean<Foo>("foo")` variant.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package org.springframework.beans.factory
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Answers
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.Mockito.verify
|
||||
import org.mockito.junit.MockitoJUnitRunner
|
||||
|
||||
/**
|
||||
* Mock object based tests for BeanFactory Kotlin extensions
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
class BeanFactoryExtensionsTests {
|
||||
|
||||
@Mock(answer = Answers.RETURNS_MOCKS)
|
||||
lateinit var bf: BeanFactory
|
||||
|
||||
@Test
|
||||
fun `getBean with KClass`() {
|
||||
bf.getBean(Foo::class)
|
||||
verify(bf, Mockito.times(1)).getBean(Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBean with reified type parameters`() {
|
||||
bf.getBean<Foo>()
|
||||
verify(bf, Mockito.times(1)).getBean(Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBean with String and reified type parameters`() {
|
||||
val name = "foo"
|
||||
bf.getBean<Foo>(name)
|
||||
verify(bf, Mockito.times(1)).getBean(name, Foo::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBean with KClass and varargs`() {
|
||||
val arg1 = "arg1"
|
||||
val arg2 = "arg2"
|
||||
bf.getBean(Foo::class, arg1, arg2)
|
||||
verify(bf, Mockito.times(1)).getBean(Foo::class.java, arg1, arg2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBean with reified type parameters and varargs`() {
|
||||
val arg1 = "arg1"
|
||||
val arg2 = "arg2"
|
||||
bf.getBean<Foo>(arg1, arg2)
|
||||
verify(bf, Mockito.times(1)).getBean(Foo::class.java, arg1, arg2)
|
||||
}
|
||||
|
||||
class Foo
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
package org.springframework.beans.factory
|
||||
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Answers
|
||||
import org.mockito.Mock
|
||||
import org.mockito.Mockito
|
||||
import org.mockito.junit.MockitoJUnitRunner
|
||||
|
||||
/**
|
||||
* Mock object based tests for ListableBeanFactory Kotlin extensions
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner::class)
|
||||
class ListenableBeanFactoryExtensionsTests {
|
||||
|
||||
@Mock(answer = Answers.RETURNS_MOCKS)
|
||||
lateinit var lbf: ListableBeanFactory
|
||||
|
||||
@Test
|
||||
fun `getBeanNamesForType with KClass`() {
|
||||
lbf.getBeanNamesForType(Foo::class)
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, true , true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeanNamesForType with KClass and Boolean`() {
|
||||
lbf.getBeanNamesForType(Foo::class, false)
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, false , true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeanNamesForType with KClass, Boolean and Boolean`() {
|
||||
lbf.getBeanNamesForType(Foo::class, false, false)
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, false , false)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeanNamesForType with reified type parameters`() {
|
||||
lbf.getBeanNamesForType<Foo>()
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, true , true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeanNamesForType with reified type parameters and Boolean`() {
|
||||
lbf.getBeanNamesForType<Foo>(false)
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, false , true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeanNamesForType with reified type parameters, Boolean and Boolean`() {
|
||||
lbf.getBeanNamesForType<Foo>(false, false)
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForType(Foo::class.java, false , false)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeansOfType with KClass`() {
|
||||
lbf.getBeansOfType(Foo::class)
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, true , true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeansOfType with KClass and Boolean`() {
|
||||
lbf.getBeansOfType(Foo::class, false)
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, false , true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeansOfType with KClass, Boolean and Boolean`() {
|
||||
lbf.getBeansOfType(Foo::class, false, false)
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, false , false)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeansOfType with reified type parameters`() {
|
||||
lbf.getBeansOfType<Foo>()
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, true , true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeansOfType with reified type parameters and Boolean`() {
|
||||
lbf.getBeansOfType<Foo>(false)
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, false , true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeansOfType with reified type parameters, Boolean and Boolean`() {
|
||||
lbf.getBeansOfType<Foo>(false, false)
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeansOfType(Foo::class.java, false , false)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeanNamesForAnnotation with KClass`() {
|
||||
lbf.getBeanNamesForAnnotation(Bar::class)
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForAnnotation(Bar::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeanNamesForAnnotation with reified type parameters`() {
|
||||
lbf.getBeanNamesForAnnotation<Bar>()
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeanNamesForAnnotation(Bar::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeansWithAnnotation with KClass`() {
|
||||
lbf.getBeansWithAnnotation(Bar::class)
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeansWithAnnotation(Bar::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getBeansWithAnnotation with reified type parameters`() {
|
||||
lbf.getBeansWithAnnotation<Bar>()
|
||||
Mockito.verify(lbf, Mockito.times(1)).getBeansWithAnnotation(Bar::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `findAnnotationOnBean with String and KClass`() {
|
||||
val name = "bar"
|
||||
lbf.findAnnotationOnBean(name, Bar::class)
|
||||
Mockito.verify(lbf, Mockito.times(1)).findAnnotationOnBean(name, Bar::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `findAnnotationOnBean with String and reified type parameters`() {
|
||||
val name = "bar"
|
||||
lbf.findAnnotationOnBean<Bar>(name)
|
||||
Mockito.verify(lbf, Mockito.times(1)).findAnnotationOnBean(name, Bar::class.java)
|
||||
}
|
||||
|
||||
class Foo
|
||||
|
||||
annotation class Bar
|
||||
}
|
||||
@@ -16,13 +16,8 @@
|
||||
|
||||
package org.springframework.beans.factory.annotation
|
||||
|
||||
import java.lang.reflect.Method
|
||||
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition
|
||||
import org.springframework.tests.sample.beans.TestBean
|
||||
@@ -30,7 +25,7 @@ import org.springframework.tests.sample.beans.TestBean
|
||||
import org.junit.Assert.*
|
||||
|
||||
/**
|
||||
* Tests for Kotlin support with [@Autowired].
|
||||
* Tests for Kotlin support with [Autowired].
|
||||
*
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user