@ -18,7 +18,6 @@ package org.springframework.context.annotation
@@ -18,7 +18,6 @@ package org.springframework.context.annotation
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.assertj.core.api.ThrowableAssert
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.BeanRegistrarDsl
import org.springframework.beans.factory.InitializingBean
@ -26,6 +25,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException
@@ -26,6 +25,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException
import org.springframework.beans.factory.config.BeanDefinition
import org.springframework.beans.factory.getBean
import org.springframework.beans.factory.support.RootBeanDefinition
import org.springframework.mock.env.MockEnvironment
import java.util.function.Supplier
/ * *
@ -37,10 +37,13 @@ class BeanRegistrarDslConfigurationTests {
@@ -37,10 +37,13 @@ class BeanRegistrarDslConfigurationTests {
@Test
fun beanRegistrar ( ) {
val context = AnnotationConfigApplicationContext ( BeanRegistrarKotlinConfiguration :: class . java )
val context = AnnotationConfigApplicationContext ( )
context . register ( BeanRegistrarKotlinConfiguration :: class . java )
context . environment = MockEnvironment ( ) . withProperty ( " hello.world " , " Hello World! " )
context . refresh ( )
assertThat ( context . getBean < Bar > ( ) . foo ) . isEqualTo ( context . getBean < Foo > ( ) )
assertThat ( context . getBean < Foo > ( " foo " ) ) . isEqualTo ( context . getBean < Foo > ( " fooAlias " ) )
assertThatThrownBy ( ThrowableAssert . ThrowingCallable { context . getBean < Baz > ( ) } ) . isInstanceOf ( NoSuchBeanDefinitionException :: class . java )
assertThatThrownBy { context . getBean < Baz > ( ) } . isInstanceOf ( NoSuchBeanDefinitionException :: class . java )
assertThat ( context . getBean < Init > ( ) . initialized ) . isTrue ( )
val beanDefinition = context . getBeanDefinition ( " bar " )
assertThat ( beanDefinition . scope ) . isEqualTo ( BeanDefinition . SCOPE _PROTOTYPE )
@ -53,7 +56,8 @@ class BeanRegistrarDslConfigurationTests {
@@ -53,7 +56,8 @@ class BeanRegistrarDslConfigurationTests {
fun beanRegistrarWithProfile ( ) {
val context = AnnotationConfigApplicationContext ( )
context . register ( BeanRegistrarKotlinConfiguration :: class . java )
context . getEnvironment ( ) . addActiveProfile ( " baz " )
context . environment = MockEnvironment ( ) . withProperty ( " hello.world " , " Hello World! " )
context . environment . addActiveProfile ( " baz " )
context . refresh ( )
assertThat ( context . getBean < Bar > ( ) . foo ) . isEqualTo ( context . getBean < Foo > ( ) )
assertThat ( context . getBean < Baz > ( ) . message ) . isEqualTo ( " Hello World! " )
@ -101,7 +105,7 @@ class BeanRegistrarDslConfigurationTests {
@@ -101,7 +105,7 @@ class BeanRegistrarDslConfigurationTests {
Bar ( bean < Foo > ( ) )
}
profile ( " baz " ) {
registerBean { Baz ( " Hello World! " ) }
registerBean { Baz ( env . getRequiredProperty ( " hello.world " ) ) }
}
registerBean < Init > ( )
registerBean ( :: booFactory , " fooFactory " )
@ -113,11 +117,7 @@ class BeanRegistrarDslConfigurationTests {
@@ -113,11 +117,7 @@ class BeanRegistrarDslConfigurationTests {
private class GenericBeanRegistrar : BeanRegistrarDsl ( {
registerBean < Supplier < Foo > > ( name = " fooSupplier " ) {
object : Supplier < Foo > {
override fun get ( ) : Foo {
return Foo ( )
}
}
Supplier < Foo > { Foo ( ) }
}
} )