Skip to content

Commit 41ad303

Browse files
author
Raghav Jha
committed
Evaluation Article
1 parent 08dbab4 commit 41ad303

5 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.spring.di;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class DepartmentService {
7+
public String getDepartment() {
8+
return "Management";
9+
}
10+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.spring.di;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component("employeeControllerWithConstructorBasedDI")
7+
public class EmployeeControllerWithConstructorBasedDI {
8+
9+
private DepartmentService departmentService;
10+
11+
@Autowired
12+
public EmployeeControllerWithConstructorBasedDI(DepartmentService departmentService) {
13+
this.departmentService = departmentService;
14+
}
15+
16+
public String getEmployeeDetails() {
17+
return "Employee: A, Department: " + departmentService.getDepartment();
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.spring.di;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component("employeeControllerWithSetterBasedDI")
7+
public class EmployeeControllerWithSetterBasedDI {
8+
private DepartmentService departmentService;
9+
10+
@Autowired
11+
public void setDepartmentService(DepartmentService departmentService) {
12+
this.departmentService = departmentService;
13+
}
14+
15+
public String getEmployeeDetails() {
16+
return "Employee: A, Department: " + departmentService.getDepartment();
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
6+
<context:component-scan base-package="com.baeldung.spring.di"/>
7+
</beans>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.spring.di;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.context.support.ClassPathXmlApplicationContext;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
@RunWith(SpringJUnit4ClassRunner.class)
13+
public class DepdencyInjectionUnitTest {
14+
15+
private ApplicationContext context;
16+
17+
@Before
18+
public void oneTimeSetup() {
19+
context = new ClassPathXmlApplicationContext("spring-di.xml");
20+
}
21+
22+
@Test
23+
public void givenConstructorBasedDI_ThenDependencyValid() {
24+
EmployeeControllerWithConstructorBasedDI employee = (EmployeeControllerWithConstructorBasedDI) context
25+
.getBean("employeeControllerWithConstructorBasedDI");
26+
assertEquals("Employee: A, Department: Management", employee.getEmployeeDetails());
27+
}
28+
29+
@Test
30+
public void givenSetterBasedDI_ThenDependencyValid() {
31+
EmployeeControllerWithSetterBasedDI employee = (EmployeeControllerWithSetterBasedDI) context
32+
.getBean("employeeControllerWithSetterBasedDI");
33+
assertEquals("Employee: A, Department: Management", employee.getEmployeeDetails());
34+
}
35+
}

0 commit comments

Comments
 (0)