BAEL-5734 - Mocking private fields (#14085)
* BAEL-5734 - Mocking private fields * BAEL-5734 - reducing spring version to make it compatible
This commit is contained in:
parent
3ddf5fa6a4
commit
104df66ef8
|
@ -23,11 +23,18 @@
|
|||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring-test.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<datafaker.version>1.6.0</datafaker.version>
|
||||
<jackson.version>2.13.4</jackson.version>
|
||||
<spring-test.version>5.3.25</spring-test.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,10 @@
|
|||
package com.baeldung.mockprivate;
|
||||
|
||||
public class MockService {
|
||||
|
||||
private final Person person = new Person("John Doe");
|
||||
|
||||
public String getName() {
|
||||
return person.getName();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.mockprivate;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final String name;
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package com.baeldung.mockprivate;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.commons.util.ReflectionUtils;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class MockServiceUnitTest {
|
||||
|
||||
private Person mockedPerson;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp(){
|
||||
mockedPerson = mock(Person.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNameChangedWithReflection_whenGetName_thenReturnName() throws Exception {
|
||||
Class<?> mockServiceClass = Class.forName("com.baeldung.mockprivate.MockService");
|
||||
MockService mockService = (MockService) mockServiceClass.getDeclaredConstructor().newInstance();
|
||||
Field field = mockServiceClass.getDeclaredField("person");
|
||||
field.setAccessible(true);
|
||||
field.set(mockService, mockedPerson);
|
||||
|
||||
when(mockedPerson.getName()).thenReturn("Jane Doe");
|
||||
|
||||
Assertions.assertEquals("Jane Doe", mockService.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNameChangedWithReflectionUtils_whenGetName_thenReturnName() throws Exception {
|
||||
MockService mockService = new MockService();
|
||||
Field field = ReflectionUtils
|
||||
.findFields(MockService.class, f -> f.getName().equals("person"),
|
||||
ReflectionUtils.HierarchyTraversalMode.TOP_DOWN)
|
||||
.get(0);
|
||||
|
||||
field.setAccessible(true);
|
||||
field.set(mockService, mockedPerson);
|
||||
|
||||
when(mockedPerson.getName()).thenReturn("Jane Doe");
|
||||
|
||||
Assertions.assertEquals("Jane Doe", mockService.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenNameChangedWithReflectionTestUtils_whenGetName_thenReturnName() throws Exception {
|
||||
MockService mockService = new MockService();
|
||||
|
||||
ReflectionTestUtils.setField(mockService, "person", mockedPerson);
|
||||
|
||||
when(mockedPerson.getName()).thenReturn("Jane Doe");
|
||||
Assertions.assertEquals("Jane Doe", mockService.getName());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue