BAEL-2293: Guide to ReflectionTestUtils and uses in Unit Testing (#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
This commit is contained in:
parent
5f91423fcb
commit
0cd4d76c15
|
@ -0,0 +1,23 @@
|
|||
package org.baeldung.reflectiontestutils.repository;
|
||||
|
||||
public class Employee {
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String employeeToString() {
|
||||
return "id: " + getId() + "; name: " + getName();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package org.baeldung.reflectiontestutils.repository;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class EmployeeService {
|
||||
@Autowired
|
||||
private HRService hrService;
|
||||
|
||||
public String findEmployeeStatus(Integer employeeId) {
|
||||
return "Employee " + employeeId + " status: " + hrService.getEmployeeStatus(employeeId);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package org.baeldung.reflectiontestutils.repository;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class HRService {
|
||||
|
||||
public String getEmployeeStatus(Integer employeeId) {
|
||||
return "Inactive";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package org.baeldung.reflectiontestutils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.baeldung.reflectiontestutils.repository.Employee;
|
||||
import org.baeldung.reflectiontestutils.repository.EmployeeService;
|
||||
import org.baeldung.reflectiontestutils.repository.HRService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ReflectionTestUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenNonPublicField_thenReflectionTestUtilsSetField() {
|
||||
Employee employee = new Employee();
|
||||
ReflectionTestUtils.setField(employee, "id", 1);
|
||||
assertTrue(employee.getId().equals(1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNonPublicMethod_thenReflectionTestUtilsInvokeMethod() {
|
||||
Employee employee = new Employee();
|
||||
ReflectionTestUtils.setField(employee, "id", 1);
|
||||
employee.setName("Smith, John");
|
||||
assertTrue(ReflectionTestUtils.invokeMethod(employee, "employeeToString").equals("id: 1; name: Smith, John"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInjectingMockOfDependency_thenReflectionTestUtilsSetField() {
|
||||
Employee employee = new Employee();
|
||||
ReflectionTestUtils.setField(employee, "id", 1);
|
||||
employee.setName("Smith, John");
|
||||
|
||||
HRService hrService = mock(HRService.class);
|
||||
when(hrService.getEmployeeStatus(employee.getId())).thenReturn("Active");
|
||||
EmployeeService employeeService = new EmployeeService();
|
||||
|
||||
// Inject mock into the private field
|
||||
ReflectionTestUtils.setField(employeeService, "hrService", hrService);
|
||||
assertEquals("Employee " + employee.getId() + " status: Active", employeeService.findEmployeeStatus(employee.getId()));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue