forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-5734 - Mocking private fields (eugenp#14085)
* BAEL-5734 - Mocking private fields * BAEL-5734 - reducing spring version to make it compatible
- Loading branch information
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
testing-modules/mocks-2/src/main/java/com/baeldung/mockprivate/MockService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
testing-modules/mocks-2/src/main/java/com/baeldung/mockprivate/Person.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
testing-modules/mocks-2/src/test/java/com/baeldung/mockprivate/MockServiceUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |