The test for the public method getA() works as expected, but the test for the private method getB() does not return the mocked value "b", but "B", which corresponds to the actual return value of the method getB(). How must the test to getB() be adjusted so that the mocked value "b" is returned?
public class Letters {
public String getA() {
return "A";
}
private String getB() {
return "B";
}
}
class LettersTest {
Letters mockedLetters = mock(Letters.class);
@Test
void getA() {
when(mockedLetters.getA()).thenReturn("a");
assertThat(mockedLetters.getA()).isEqualTo("a"); // True.
// To check if ReflectionTestUtils actually sets the value in mockedLetters.
when(ReflectionTestUtils.invokeMethod(mockedLetters, "getA")).thenReturn("aa");
assertThat(mockedLetters.getA()).isEqualTo("aa"); // True.
}
@Test
void getB() {
when(ReflectionTestUtils.invokeMethod(mockedLetters, "getB")).thenReturn("b");
assertThat((String)ReflectionTestUtils.invokeMethod(mockedLetters, "getB")).isEqualTo("b"); // False; expected: "b", but was: "B".
}
}