Skip to content

Commit 96004de

Browse files
adasioomaibin
authored andcommitted
BAEL-923 init class not managed by Spring with Spring Bean (eugenp#1906)
* adam.zawada@gmail.com - Different Types of Bean Injection in Spring * adam.zawada@gmail.com - Different Types of Bean Injection in Spring switch to Java based configuration * BAEL-895 calculate the period/duration between two dates in Java 8 * clean old PR * BAEL-923 How to inject a value from properties to a class not managed by Spring? * clean PR * BAEL-923 init class not managed by Spring with Spring Bean * clean previous PR * added mockito spring boot for test dependency
1 parent 689a72d commit 96004de

5 files changed

Lines changed: 98 additions & 0 deletions

File tree

‎spring-core/pom.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@
5858
<artifactId>lombok</artifactId>
5959
<version>${lombok.version}</version>
6060
</dependency>
61+
<dependency>
62+
<groupId>org.springframework.boot</groupId>
63+
<artifactId>spring-boot-test</artifactId>
64+
<version>${mockito.spring.boot.version}</version>
65+
<scope>test</scope>
66+
</dependency>
6167
</dependencies>
6268

6369
<build>
@@ -112,6 +118,7 @@
112118

113119
<properties>
114120
<mockito.version>1.10.19</mockito.version>
121+
<mockito.spring.boot.version>1.4.4.RELEASE</mockito.spring.boot.version>
115122
<spring.version>4.3.4.RELEASE</spring.version>
116123
<javax.inject.version>1</javax.inject.version>
117124
<guava.version>20.0</guava.version>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.value;
2+
3+
public class ClassNotManagedBySpring {
4+
5+
private String customVariable;
6+
private String anotherCustomVariable;
7+
8+
public ClassNotManagedBySpring(String someInitialValue, String anotherManagedValue) {
9+
this.customVariable = someInitialValue;
10+
this.anotherCustomVariable = anotherManagedValue;
11+
}
12+
13+
public String getCustomVariable() {
14+
return customVariable;
15+
}
16+
17+
public void setCustomVariable(String customVariable) {
18+
this.customVariable = customVariable;
19+
}
20+
21+
public String getAnotherCustomVariable() {
22+
return anotherCustomVariable;
23+
}
24+
25+
public void setAnotherCustomVariable(String anotherCustomVariable) {
26+
this.anotherCustomVariable = anotherCustomVariable;
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.value;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class InitializerBean {
8+
9+
private String someInitialValue;
10+
private String anotherManagedValue;
11+
12+
public InitializerBean(@Value("someInitialValue") String someInitialValue, @Value("anotherValue") String anotherManagedValue) {
13+
this.someInitialValue = someInitialValue;
14+
this.anotherManagedValue = anotherManagedValue;
15+
}
16+
17+
public ClassNotManagedBySpring initClass() {
18+
return new ClassNotManagedBySpring(this.someInitialValue, this.anotherManagedValue);
19+
}
20+
21+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
someInitialValue=This is only sample value
2+
anotherValue=Another configured value
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.value;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.boot.test.mock.mockito.MockBean;
7+
import org.springframework.test.context.junit4.SpringRunner;
8+
9+
import static junit.framework.TestCase.assertEquals;
10+
import static org.mockito.Mockito.when;
11+
12+
@RunWith(SpringRunner.class)
13+
public class ClassNotManagedBySpringTest {
14+
15+
@MockBean
16+
private InitializerBean initializerBean;
17+
18+
@Before
19+
public void init() {
20+
when(initializerBean.initClass())
21+
.thenReturn(new ClassNotManagedBySpring("This is only sample value", "Another configured value"));
22+
}
23+
24+
@Test
25+
public void givenInitializerBean_whenInvokedInitClass_shouldInitialize() throws Exception {
26+
27+
//given
28+
ClassNotManagedBySpring classNotManagedBySpring = initializerBean.initClass();
29+
30+
//when
31+
String initializedValue = classNotManagedBySpring.getCustomVariable();
32+
String anotherCustomVariable = classNotManagedBySpring.getAnotherCustomVariable();
33+
34+
//then
35+
assertEquals("This is only sample value", initializedValue);
36+
assertEquals("Another configured value", anotherCustomVariable);
37+
38+
}
39+
40+
}

0 commit comments

Comments
 (0)