From 104df66ef8af4c43fe135588595e4788278cfce2 Mon Sep 17 00:00:00 2001 From: Abhinav Pandey Date: Mon, 5 Jun 2023 09:12:47 +0530 Subject: [PATCH] BAEL-5734 - Mocking private fields (#14085) * BAEL-5734 - Mocking private fields * BAEL-5734 - reducing spring version to make it compatible --- testing-modules/mocks-2/pom.xml | 7 +++ .../com/baeldung/mockprivate/MockService.java | 10 +++ .../java/com/baeldung/mockprivate/Person.java | 14 +++++ .../mockprivate/MockServiceUnitTest.java | 62 +++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 testing-modules/mocks-2/src/main/java/com/baeldung/mockprivate/MockService.java create mode 100644 testing-modules/mocks-2/src/main/java/com/baeldung/mockprivate/Person.java create mode 100644 testing-modules/mocks-2/src/test/java/com/baeldung/mockprivate/MockServiceUnitTest.java diff --git a/testing-modules/mocks-2/pom.xml b/testing-modules/mocks-2/pom.xml index e47649ca33..22a5198b8c 100644 --- a/testing-modules/mocks-2/pom.xml +++ b/testing-modules/mocks-2/pom.xml @@ -23,11 +23,18 @@ jackson-databind ${jackson.version} + + org.springframework + spring-test + ${spring-test.version} + test + 1.6.0 2.13.4 + 5.3.25 \ No newline at end of file diff --git a/testing-modules/mocks-2/src/main/java/com/baeldung/mockprivate/MockService.java b/testing-modules/mocks-2/src/main/java/com/baeldung/mockprivate/MockService.java new file mode 100644 index 0000000000..ab5624d973 --- /dev/null +++ b/testing-modules/mocks-2/src/main/java/com/baeldung/mockprivate/MockService.java @@ -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(); + } +} diff --git a/testing-modules/mocks-2/src/main/java/com/baeldung/mockprivate/Person.java b/testing-modules/mocks-2/src/main/java/com/baeldung/mockprivate/Person.java new file mode 100644 index 0000000000..405b9d58dd --- /dev/null +++ b/testing-modules/mocks-2/src/main/java/com/baeldung/mockprivate/Person.java @@ -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; + } +} diff --git a/testing-modules/mocks-2/src/test/java/com/baeldung/mockprivate/MockServiceUnitTest.java b/testing-modules/mocks-2/src/test/java/com/baeldung/mockprivate/MockServiceUnitTest.java new file mode 100644 index 0000000000..64367a2290 --- /dev/null +++ b/testing-modules/mocks-2/src/test/java/com/baeldung/mockprivate/MockServiceUnitTest.java @@ -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()); + } + +} \ No newline at end of file