BAEL-923 init class not managed by Spring with Spring Bean (#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
This commit is contained in:
adasioo 2017-05-29 22:21:03 +02:00 committed by maibin
parent 689a72ddf2
commit 96004deac1
5 changed files with 98 additions and 0 deletions

View File

@ -58,6 +58,12 @@
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>${mockito.spring.boot.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -112,6 +118,7 @@
<properties>
<mockito.version>1.10.19</mockito.version>
<mockito.spring.boot.version>1.4.4.RELEASE</mockito.spring.boot.version>
<spring.version>4.3.4.RELEASE</spring.version>
<javax.inject.version>1</javax.inject.version>
<guava.version>20.0</guava.version>

View File

@ -0,0 +1,28 @@
package com.baeldung.value;
public class ClassNotManagedBySpring {
private String customVariable;
private String anotherCustomVariable;
public ClassNotManagedBySpring(String someInitialValue, String anotherManagedValue) {
this.customVariable = someInitialValue;
this.anotherCustomVariable = anotherManagedValue;
}
public String getCustomVariable() {
return customVariable;
}
public void setCustomVariable(String customVariable) {
this.customVariable = customVariable;
}
public String getAnotherCustomVariable() {
return anotherCustomVariable;
}
public void setAnotherCustomVariable(String anotherCustomVariable) {
this.anotherCustomVariable = anotherCustomVariable;
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.value;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class InitializerBean {
private String someInitialValue;
private String anotherManagedValue;
public InitializerBean(@Value("someInitialValue") String someInitialValue, @Value("anotherValue") String anotherManagedValue) {
this.someInitialValue = someInitialValue;
this.anotherManagedValue = anotherManagedValue;
}
public ClassNotManagedBySpring initClass() {
return new ClassNotManagedBySpring(this.someInitialValue, this.anotherManagedValue);
}
}

View File

@ -0,0 +1,2 @@
someInitialValue=This is only sample value
anotherValue=Another configured value

View File

@ -0,0 +1,40 @@
package com.baeldung.value;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import static junit.framework.TestCase.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(SpringRunner.class)
public class ClassNotManagedBySpringTest {
@MockBean
private InitializerBean initializerBean;
@Before
public void init() {
when(initializerBean.initClass())
.thenReturn(new ClassNotManagedBySpring("This is only sample value", "Another configured value"));
}
@Test
public void givenInitializerBean_whenInvokedInitClass_shouldInitialize() throws Exception {
//given
ClassNotManagedBySpring classNotManagedBySpring = initializerBean.initClass();
//when
String initializedValue = classNotManagedBySpring.getCustomVariable();
String anotherCustomVariable = classNotManagedBySpring.getAnotherCustomVariable();
//then
assertEquals("This is only sample value", initializedValue);
assertEquals("Another configured value", anotherCustomVariable);
}
}