Powermock

Мокаем статик финал поле - ссылка
PowerMock with Mockito
Powermock wiki
Mockito + Powermock examples

Mock private method

//Example class
public class ClassForTest {
    private void change(String param1) {
        System.out.println("call: \"change\"");
        privateMethod(param1);
        System.out.println("print return param: "+ privateMethod2());
    }
 
    private void privateMethod(String param1) {
        System.out.println("call: \"privateMethod\"");
        System.out.println("print param: " + param1);
    }
 
    private String privateMethod2() {
        System.out.println("call: \"privateMethod2\"");
        return "param method \"privateMethod2\"";
    }
}
 
//Example test for Easymock
import static org.powermock.api.easymock.PowerMock.*;
 
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassForTest.class)
public class TestClassForTest {
 
    @Test
    public void testName() throws Exception {
        ClassForTest tested = createPartialMock(ClassForTest.class, "privateMethod", "privateMethod2");
 
        expectPrivate(tested, "privateMethod", new Class<?>[]{String.class}, EasyMock.anyObject(String.class));
        expectPrivate(tested, "privateMethod2").andReturn("mock");
        replay(tested);
 
        Method method = ClassForTest.class.getDeclaredMethod("change", String.class);
        method.setAccessible(true);
        method.invoke(tested, "call private method");
    }
}
 
//Example test for Mocito
import static org.powermock.api.easymock.PowerMock.*;
 
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassForTest.class)
public class TestClassForTest {
 
    @Test
    public void testName() throws Exception {
         ClassForTest tested = new ClassForTest();
         String param1 = "param";
         verifyPrivate(tested).invoke("privateMethod", param1 );
         verifyPrivate(tested).invoke("privateMethod2");
    }
}
Пока не указано иное, содержимое этой страницы распространяется по лицензии Creative Commons Attribution-ShareAlike 3.0 License