Мой юнит-тест:
@Test
public void testDoSomething() {
Method m = MyClass.class.getDeclaredMethod("methodToBeMocked");
m.setAccessible(true);
MyClass myClass = spy(new MyClass());
//m.invoke(myClass); // Calling this invokes the private method correctly
when(myClass.methodToBeMocked()).thenReturn("DummyReturn"); // This line throws the compiler error
myClass.doSomething(); // This is the method I'm trying to test
}
Это ошибка компилятора, которую я получаю:
The method methodToBeMocked() from the type MyClass is not visible
Это MyClass
public MyClass {
public MyClass() {}
public void doSomething() {
..
methodToBeMocked();
..
}
private String methodToBeMocked() { // Need to mock return value
return "Default";
}
}