Skip to content

Commit

Permalink
BAEL-5734 - Mocking private fields (eugenp#14085)
Browse files Browse the repository at this point in the history
* BAEL-5734 - Mocking private fields

* BAEL-5734 - reducing spring version to make it compatible
  • Loading branch information
abh1navv authored Jun 5, 2023
1 parent 3ddf5fa commit 104df66
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
7 changes: 7 additions & 0 deletions testing-modules/mocks-2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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());
}

}

0 comments on commit 104df66

Please sign in to comment.