@@ -18,14 +18,14 @@ package org.springframework.context.annotation
18
18
19
19
import org.assertj.core.api.Assertions.assertThat
20
20
import org.assertj.core.api.Assertions.assertThatThrownBy
21
- import org.assertj.core.api.ThrowableAssert
22
21
import org.junit.jupiter.api.Test
23
22
import org.springframework.beans.factory.BeanRegistrarDsl
24
23
import org.springframework.beans.factory.InitializingBean
25
24
import org.springframework.beans.factory.NoSuchBeanDefinitionException
26
25
import org.springframework.beans.factory.config.BeanDefinition
27
26
import org.springframework.beans.factory.getBean
28
27
import org.springframework.beans.factory.support.RootBeanDefinition
28
+ import org.springframework.mock.env.MockEnvironment
29
29
import java.util.function.Supplier
30
30
31
31
/* *
@@ -37,10 +37,13 @@ class BeanRegistrarDslConfigurationTests {
37
37
38
38
@Test
39
39
fun beanRegistrar () {
40
- val context = AnnotationConfigApplicationContext (BeanRegistrarKotlinConfiguration ::class .java)
40
+ val context = AnnotationConfigApplicationContext ()
41
+ context.register(BeanRegistrarKotlinConfiguration ::class .java)
42
+ context.environment = MockEnvironment ().withProperty(" hello.world" , " Hello World!" )
43
+ context.refresh()
41
44
assertThat(context.getBean<Bar >().foo).isEqualTo(context.getBean<Foo >())
42
45
assertThat(context.getBean<Foo >(" foo" )).isEqualTo(context.getBean<Foo >(" fooAlias" ))
43
- assertThatThrownBy( ThrowableAssert . ThrowingCallable { context.getBean<Baz >() }) .isInstanceOf(NoSuchBeanDefinitionException ::class .java)
46
+ assertThatThrownBy { context.getBean<Baz >() }.isInstanceOf(NoSuchBeanDefinitionException ::class .java)
44
47
assertThat(context.getBean<Init >().initialized).isTrue()
45
48
val beanDefinition = context.getBeanDefinition(" bar" )
46
49
assertThat(beanDefinition.scope).isEqualTo(BeanDefinition .SCOPE_PROTOTYPE )
@@ -53,7 +56,8 @@ class BeanRegistrarDslConfigurationTests {
53
56
fun beanRegistrarWithProfile () {
54
57
val context = AnnotationConfigApplicationContext ()
55
58
context.register(BeanRegistrarKotlinConfiguration ::class .java)
56
- context.getEnvironment().addActiveProfile(" baz" )
59
+ context.environment = MockEnvironment ().withProperty(" hello.world" , " Hello World!" )
60
+ context.environment.addActiveProfile(" baz" )
57
61
context.refresh()
58
62
assertThat(context.getBean<Bar >().foo).isEqualTo(context.getBean<Foo >())
59
63
assertThat(context.getBean<Baz >().message).isEqualTo(" Hello World!" )
@@ -101,7 +105,7 @@ class BeanRegistrarDslConfigurationTests {
101
105
Bar (bean<Foo >())
102
106
}
103
107
profile("baz") {
104
- registerBean { Baz (" Hello World !" ) }
108
+ registerBean { Baz (env.getRequiredProperty("hello.world") ) }
105
109
}
106
110
registerBean<Init >()
107
111
registerBean(::booFactory, "fooFactory")
@@ -113,11 +117,7 @@ class BeanRegistrarDslConfigurationTests {
113
117
114
118
private class GenericBeanRegistrar : BeanRegistrarDsl ({
115
119
registerBean<Supplier <Foo >>(name = "fooSupplier") {
116
- object : Supplier <Foo > {
117
- override fun get(): Foo {
118
- return Foo ()
119
- }
120
- }
120
+ Supplier <Foo > { Foo () }
121
121
}
122
122
})
123
123
0 commit comments