Mock private Methode mit PowerMockito

Ich benutze PowerMockito, um den privaten Methodenaufruf (privateApi) zu verspotten, aber es wird immer noch der privateApi-Aufruf ausgeführt, der wiederum einen weiteren dritten PartCall auslöst. Ich bekomme ein Problem, wenn 3rdPartyCall eine Ausnahme auslöst. Soweit ich weiß, sollte PrivateApi, wenn ich es verspotte, nicht im Detail der Methodenimplementierung auftauchen und die verspottete Antwort zurückgeben.

public class MyClient {

    public void publicApi() {
        System.out.println("In publicApi");
        int result = 0;
        try {
            result = privateApi("hello", 1);
        } catch (Exception e) {
            Assert.fail();
        }
        System.out.println("result : "+result);
        if (result == 20) {
            throw new RuntimeException("boom");
        }
    }

    private int privateApi(String whatever, int num) throws Exception {
        System.out.println("In privateAPI");
        thirdPartyCall();
        int resp = 10;
        return resp;
    }

    private void thirdPartyCall() throws Exception{
        System.out.println("In thirdPartyCall");
        //Actual WS call which may be down while running the test cases
    }
}

Hier ist der Testfall:

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClient.class)
public class MyclientTest {

    @Test(expected = RuntimeException.class)
    public void testPublicAPI() throws Exception {
        MyClient classUnderTest = PowerMockito.spy(new MyClient());
        PowerMockito.when(classUnderTest, "privateApi", anyString(), anyInt()).thenReturn(20);
        classUnderTest.publicApi();
    }
}

Console trace:

In privateAPI
In thirdPartyCall
In publicApi
result : 20

Antworten auf die Frage(2)

Ihre Antwort auf die Frage