File tree Expand file tree Collapse file tree
main/java/com/baeldung/beaninjectiontypes
test/java/com/baeldung/beaninjectiontypes/vehicle Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjectiontypes ;
2+
3+ import com .baeldung .beaninjectiontypes .vehicle .engine .Engine ;
4+ import com .baeldung .beaninjectiontypes .vehicle .engine .EngineReal ;
5+ import org .springframework .boot .SpringApplication ;
6+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
7+ import org .springframework .context .annotation .Bean ;
8+ import org .springframework .context .annotation .Configuration ;
9+
10+ @ SpringBootApplication
11+ public class Application {
12+ public static void main (String [] args ) {
13+ SpringApplication .run (Application .class , args );
14+ }
15+
16+ @ Configuration
17+ static class ContextConfiguration {
18+
19+ @ Bean
20+ public Engine engine () {
21+ return new EngineReal ();
22+ }
23+ }
24+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjectiontypes .vehicle ;
2+
3+ import com .baeldung .beaninjectiontypes .vehicle .engine .Engine ;
4+
5+ public interface Vehicle {
6+
7+ Engine getEngine ();
8+
9+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjectiontypes .vehicle ;
2+
3+ import com .baeldung .beaninjectiontypes .vehicle .engine .Engine ;
4+ import org .springframework .beans .factory .annotation .Autowired ;
5+ import org .springframework .stereotype .Component ;
6+
7+ @ Component
8+ public class VehicleConstructorInjection implements Vehicle {
9+
10+ private Engine engine ;
11+
12+ @ Autowired
13+ public VehicleConstructorInjection (Engine engine ) {
14+ this .engine = engine ;
15+ }
16+
17+ @ Override
18+ public Engine getEngine () {
19+ return engine ;
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjectiontypes .vehicle ;
2+
3+ import com .baeldung .beaninjectiontypes .vehicle .engine .Engine ;
4+ import org .springframework .beans .factory .annotation .Autowired ;
5+ import org .springframework .stereotype .Component ;
6+
7+ @ Component
8+ public class VehicleFieldInjection implements Vehicle {
9+
10+ @ Autowired
11+ private Engine engine ;
12+
13+ @ Override
14+ public Engine getEngine () {
15+ return engine ;
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjectiontypes .vehicle ;
2+
3+ import com .baeldung .beaninjectiontypes .vehicle .engine .Engine ;
4+ import org .springframework .beans .factory .annotation .Autowired ;
5+ import org .springframework .stereotype .Component ;
6+
7+ @ Component
8+ public class VehicleSetterInjection implements Vehicle {
9+
10+ private Engine engine ;
11+
12+ @ Autowired
13+ public void setEngine (Engine engine ) {
14+ this .engine = engine ;
15+ }
16+
17+ @ Override
18+ public Engine getEngine () {
19+ return engine ;
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjectiontypes .vehicle .engine ;
2+
3+ public interface Engine {
4+
5+ boolean isMock ();
6+
7+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjectiontypes .vehicle .engine ;
2+
3+ import org .springframework .stereotype .Component ;
4+
5+ @ Component
6+ public class EngineMock implements Engine {
7+
8+ @ Override
9+ public boolean isMock () {
10+ return true ;
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjectiontypes .vehicle .engine ;
2+
3+ import org .springframework .stereotype .Component ;
4+
5+ @ Component
6+ public class EngineReal implements Engine {
7+
8+ @ Override
9+ public boolean isMock () {
10+ return false ;
11+ }
12+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjectiontypes .vehicle ;
2+
3+ import com .baeldung .beaninjectiontypes .vehicle .engine .Engine ;
4+ import com .baeldung .beaninjectiontypes .vehicle .engine .EngineMock ;
5+ import org .junit .Assert ;
6+ import org .junit .Test ;
7+
8+ public class VehicleConstructorInjectionUnitTest {
9+
10+ @ Test
11+ public void whenUsingConstructorInjection_thenEngineMockIsInjected () {
12+ Engine engine = new EngineMock ();
13+ Vehicle vehicle = new VehicleConstructorInjection (engine );
14+
15+ Assert .assertTrue (vehicle .getEngine ().isMock ());
16+ }
17+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjectiontypes .vehicle ;
2+
3+ import com .baeldung .beaninjectiontypes .vehicle .engine .Engine ;
4+ import com .baeldung .beaninjectiontypes .vehicle .engine .EngineMock ;
5+ import org .junit .Assert ;
6+ import org .junit .Test ;
7+ import org .junit .runner .RunWith ;
8+ import org .springframework .beans .factory .annotation .Autowired ;
9+ import org .springframework .boot .test .context .SpringBootTest ;
10+ import org .springframework .context .annotation .Bean ;
11+ import org .springframework .context .annotation .Configuration ;
12+ import org .springframework .test .context .junit4 .SpringRunner ;
13+
14+ @ RunWith (SpringRunner .class )
15+ @ SpringBootTest
16+ public class VehicleFieldInjectionIntegrationTest {
17+
18+ @ Configuration
19+ static class ContextConfiguration {
20+
21+ @ Bean
22+ public Vehicle vehicle () {
23+ return new VehicleFieldInjection ();
24+ }
25+
26+ @ Bean
27+ public Engine engine () {
28+ return new EngineMock ();
29+ }
30+ }
31+
32+ @ Autowired
33+ private Vehicle vehicle ;
34+
35+ @ Test
36+ public void whenUsingFieldInjection_thenEngineMockIsInjected () {
37+ Assert .assertTrue (vehicle .getEngine ().isMock ());
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments