File tree Expand file tree Collapse file tree 3 files changed +58
-0
lines changed
kotlin/com/baeldung/value
test/kotlin/com/baeldung/value Expand file tree Collapse file tree 3 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com.baeldung.value
2
+
3
+ import org.springframework.beans.factory.annotation.Value
4
+ import org.springframework.stereotype.Component
5
+
6
+ @Component
7
+ class MyBean {
8
+ @Value(" \$ {some.property}" )
9
+ lateinit var propertyValue: String
10
+ }
11
+
12
+ @Component
13
+ class MyBean2 (@Value(" \$ {some.property}" ) val propertyValue : String )
14
+
15
+ @Component
16
+ class MyBean3 (@Value(" \$ {non-existent-property:spring-default}" ) val propertyValue : String )
17
+
18
+ @Component
19
+ class MyBean4 (@Value(" \$ {non-existent-property:#{null}}" ) val propertyValue : String? )
Original file line number Diff line number Diff line change 8
8
username : sa
9
9
password :
10
10
driver-class-name : org.hsqldb.jdbcDriver
11
+
12
+ some.property : " abc"
Original file line number Diff line number Diff line change
1
+ package com.baeldung.value
2
+
3
+ import com.baeldung.consoleapp.CommandLineFirstComponent
4
+ import org.assertj.core.api.Assertions.assertThat
5
+ import org.junit.Test
6
+ import org.junit.runner.RunWith
7
+ import org.springframework.beans.factory.annotation.Autowired
8
+ import org.springframework.boot.test.context.SpringBootTest
9
+ import org.springframework.test.context.junit4.SpringRunner
10
+ import kotlin.test.assertEquals
11
+
12
+ @RunWith(SpringRunner ::class )
13
+ @SpringBootTest
14
+ class ValueAnnotationIntegrationTest {
15
+
16
+ @Autowired
17
+ lateinit var myBean: MyBean
18
+
19
+ @Autowired
20
+ lateinit var myBean2: MyBean2
21
+
22
+ @Autowired
23
+ lateinit var myBean3: MyBean3
24
+
25
+ @Autowired
26
+ lateinit var myBean4: MyBean4
27
+
28
+
29
+ @Test
30
+ fun `Verify beans are getting the correct value` () {
31
+ assertEquals(" abc" , myBean.propertyValue)
32
+ assertEquals(" abc" , myBean2.propertyValue)
33
+ assertEquals(" spring-default" , myBean3.propertyValue)
34
+ assertEquals(null , myBean4.propertyValue)
35
+ }
36
+
37
+ }
You can’t perform that action at this time.
0 commit comments