Skip to content

Commit 0cd4d76

Browse files
codehunter34maibin
authored andcommitted
BAEL-2293: Guide to ReflectionTestUtils and uses in Unit Testing (eugenp#5681)
* BAEL-2293: Guide to ReflectionTestUtils and uses in Unit Testing * BAEL-2293: Guide to ReflectionTestUtils and uses in Unit Testing * BAEL-2293: Guide to ReflectionTestUtils and uses in Unit Testing * Update ReflectionTestUtilsUnitTest.java
1 parent 5f91423 commit 0cd4d76

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.reflectiontestutils.repository;
2+
3+
public class Employee {
4+
private Integer id;
5+
private String name;
6+
7+
public Integer getId() {
8+
return id;
9+
}
10+
11+
public String getName() {
12+
return name;
13+
}
14+
15+
public void setName(String name) {
16+
this.name = name;
17+
}
18+
19+
private String employeeToString() {
20+
return "id: " + getId() + "; name: " + getName();
21+
}
22+
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.baeldung.reflectiontestutils.repository;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class EmployeeService {
8+
@Autowired
9+
private HRService hrService;
10+
11+
public String findEmployeeStatus(Integer employeeId) {
12+
return "Employee " + employeeId + " status: " + hrService.getEmployeeStatus(employeeId);
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.baeldung.reflectiontestutils.repository;
2+
3+
import org.springframework.stereotype.Component;
4+
5+
@Component
6+
public class HRService {
7+
8+
public String getEmployeeStatus(Integer employeeId) {
9+
return "Inactive";
10+
}
11+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.baeldung.reflectiontestutils;
2+
3+
import static org.junit.Assert.*;
4+
import static org.mockito.Mockito.mock;
5+
6+
import org.baeldung.reflectiontestutils.repository.Employee;
7+
import org.baeldung.reflectiontestutils.repository.EmployeeService;
8+
import org.baeldung.reflectiontestutils.repository.HRService;
9+
import org.junit.Test;
10+
import org.springframework.test.util.ReflectionTestUtils;
11+
12+
import static org.mockito.Mockito.when;
13+
14+
public class ReflectionTestUtilsUnitTest {
15+
16+
@Test
17+
public void whenNonPublicField_thenReflectionTestUtilsSetField() {
18+
Employee employee = new Employee();
19+
ReflectionTestUtils.setField(employee, "id", 1);
20+
assertTrue(employee.getId().equals(1));
21+
22+
}
23+
24+
@Test
25+
public void whenNonPublicMethod_thenReflectionTestUtilsInvokeMethod() {
26+
Employee employee = new Employee();
27+
ReflectionTestUtils.setField(employee, "id", 1);
28+
employee.setName("Smith, John");
29+
assertTrue(ReflectionTestUtils.invokeMethod(employee, "employeeToString").equals("id: 1; name: Smith, John"));
30+
}
31+
32+
@Test
33+
public void whenInjectingMockOfDependency_thenReflectionTestUtilsSetField() {
34+
Employee employee = new Employee();
35+
ReflectionTestUtils.setField(employee, "id", 1);
36+
employee.setName("Smith, John");
37+
38+
HRService hrService = mock(HRService.class);
39+
when(hrService.getEmployeeStatus(employee.getId())).thenReturn("Active");
40+
EmployeeService employeeService = new EmployeeService();
41+
42+
// Inject mock into the private field
43+
ReflectionTestUtils.setField(employeeService, "hrService", hrService);
44+
assertEquals("Employee " + employee.getId() + " status: Active", employeeService.findEmployeeStatus(employee.getId()));
45+
}
46+
}

0 commit comments

Comments
 (0)