File tree Expand file tree Collapse file tree
test/java/com/baeldung/value Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 >
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>
Original file line number Diff line number Diff line change 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+ }
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+ 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+ }
Original file line number Diff line number Diff line change 1+ someInitialValue =This is only sample value
2+ anotherValue =Another configured value
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments