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:
Abhinav Pandey 2023-06-05 09:12:47 +05:30 committed by GitHub
parent 3ddf5fa6a4
commit 104df66ef8
4 changed files with 93 additions and 0 deletions

View File

@ -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>

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}