PowerMock + Emma - code coverage showing 0% for private static methods and other methods too [duplicate]

烂漫一生 提交于 2019-12-19 11:16:07

问题


I have taken a reference of PowerMock from : Mock private method using PowerMockito and applied the same logic here. Also, I installed EMMA (open source tool) in eclipse/STS, but when I run the code I see zero % code coverage. why ?

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 static int privateApi(String whatever, int num) throws Exception {
        System.out.println("In privateAPI");
        thirdPartyCall();
        int resp = 10;
        return resp;
    }

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

MyClientTest.java

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

    @Test
    public void testPublicAPI() throws Exception {
        PowerMockito.mockStatic(MyClient.class);

        //PowerMockito.doReturn(10).when(MyClient.class, "privateApi", anyString(), anyInt());
        PowerMockito.when(MyClient.class,"privateApi", anyString(), anyInt()).thenReturn(anyInt());
    }
}

Actual Code Coverage:

pom.xml

<dependencies>
        <!-- Power Mock -->
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>1.7.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>1.7.4</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4-rule-agent</artifactId>
            <version>1.7.4</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-core</artifactId>
            <version>1.7.4</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

回答1:


If you are constructing a Spy or Mock you are not invoking the actual code under test. The point of spies is to be able to verify() them, in order to check your code behaved correctly by invoking the right callbacks or methods. In the case of mocks the point is to steer code down a particular control flow path and also to verify() expected interactions with the mock.

Since your test case invokes the test method on a spy, it is therefore no wonder your code coverage is exactly 0%. If you were to verify your interactions with the mocked method, you'd probably find that none happened.

What you want to do instead is to setup your mocks but invoke the actual code under test 'the normal way'. The idea is to prime the execution environment, then invoke the tested method call 'normally', and finally observe what actually happened. That last bit consists of normal assertions on the produced output, verification of expected interactions (both that these took place, and also that these involved the expected arguments/values).

Change your test code:

MyClient classUnderTest = PowerMockito.spy(new MyClient());

To:

MyClient classUnderTest = new MyClient();

And watch the code coverage.



来源:https://stackoverflow.com/questions/52064126/powermock-emma-code-coverage-showing-0-for-private-static-methods-and-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!