Unable to get Jacoco to work with Powermockito using offline instrumentation

匿名 (未验证) 提交于 2019-12-03 01:14:02

问题:

Given that Jacoco doesn't play nicely with PowerMockito when instrumenting "on the fly", I've been trying to configure offline instrumentation in the hope this will give me proper unit test coverage for classes that use PowerMockito.

I've setup my pom as below but I still get zero % coverage on my test class. Any help much appreciated as it's driving me slowly bonkers!

4.0.0mandyjacoco-testwar1.0-SNAPSHOTjacoco-test Maven Webapphttp://maven.apache.org1.5.40.7.1.201405082137org.jacocoorg.jacoco.agentruntime${jacoco.version}testorg.powermockpowermock-module-junit4${powermock.version}testorg.powermockpowermock-api-mockito${powermock.version}testjunitjunit4.10testorg.apache.maven.pluginsmaven-compiler-plugin2.3.21.61.6org.jacocojacoco-maven-plugin${jacoco.version}instrumentprocess-classesinstrumentrestore-reportprepare-packagerestore-instrumented-classesreportorg.apache.maven.pluginsmaven-surefire-plugin2.17target/jacoco.execjacoco-test

here is my class under test:

public class Utils {      private Utils() {      }      public static String say(String s) {         return "hello:"+s;     } } 

here is my test:

@RunWith(PowerMockRunner.class)  @PrepareOnlyThisForTest(Utils.class) @PowerMockIgnore("org.jacoco.agent.rt.*") public class UtilsTest {      @Test     public void testSay() throws Exception {         PowerMockito.mockStatic(Utils.class);         Mockito.when(Utils.say(Mockito.anyString())).thenReturn("hello:mandy");         assertEquals("hello:mandy", Utils.say("sid"));     }  } 

I run mvn clean install which generates the jacoco.exe

Coverage report (generated from jacoco.exec using an ant script ):-

回答1:

This pom worked for me:

 final-namemaven-compiler-plugin1.81.8org.apache.maven.pluginsmaven-surefire-plugin2.18.1target/jacoco.execorg.jacocojacoco-maven-plugin0.7.2.201409121644default-instrumentinstrumentdefault-restore-instrumented-classesrestore-instrumented-classesdefault-reportprepare-packagereport

See this link.



回答2:

I saw the same behavior, though after following the thread on the GitHub issue it seems to be fixed in 1.6.5, which proved true for me.

Hopefully this will save someone a headache later :).

Working configuration with:

  • jacoco-maven-plugin 0.7.7.201606060606
  • powermock 1.6.5

I am not using offline instrumentation.



回答3:

I made it work using the javaagent of PowerMock. see here: https://code.google.com/p/powermock/wiki/PowerMockAgent

Remove the @RunWith annotations, put the PowerMockRule as described in the link above. Make it public.

Put the following line in the maven-surefire-plugin configuration:

-javaagent:${org.powermock:powermock-module-javaagent:jar} 

(used the technique described here : Can I use the path to a Maven dependency as a property?)

4.0.0com.stackoverflowq23598722.0-SNAPSHOTq2359872${org.jmockit:jmockit:jar}org.jmockitjmockit1.11generate-sourcesorg.apache.maven.pluginsmaven-dependency-plugin2.3propertiesorg.codehaus.mojoexec-maven-plugin1.2execgenerate-sourcesechopath to jar=${org.jmockit:jmockit:jar}my.lib=${my.lib}


回答4:

I was also facing the same issue. I was able to generate report partially. I have used both these tags for my test cases @RunWith(PowerMockRunner.class) @PrepareForTest({}). And the report was not getting generated for the test cases where i used the above mentioned tags. But for one of the test case there was only this tag @RunWith(PowerMockRunner.class). Somehow the report was getting generated for that case. And also i have never used offline instrumentation. When i tried using the offline instrumentation i was getting error saying that the class was already instrumented. I tried various scenarios and followed various links but could not generate the report. Finally as per the above comment i upgraded my version of powermock from 1.5.5 to 1.6.5 and i was able to generate the report. Following is my pom.xml entry

     org.jacocojacoco-maven-plugin0.7.5.201505241946pre-unit-testprepare-agent${basedir}/target/jacoco.execdefault-reportverifyreport${basedir}/target/jacoco.exec${basedir}/target/jacoco-ut

Following is my entry in pom .xml for maven-surefire-plugin

    org.apache.maven.pluginsmaven-surefire-plugin@{argLine}falsetrue

@{argLine} was set as a property

-noverify -Xms512m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=512m

And upgraded my powermock version from 1.5.5 to 1.6.5 . Finally i could see my report generation for the classes where i used the following tags in my test cases @RunWith(PowerMockRunner.class) @PrepareForTest({})



回答5:

I ended up using offline instrumentation and Jacoco (similar to what you have done) in conjunction with sonar and I was able to get the coverage numbers from that.



回答6:

I used Jacoco offline instrumentation and after execution of test restore ed original classes with help of "restore-instrumented-classes" goal. My JaCoCo configuration look like this:

 org.jacocojacoco-maven-plugin0.7.9default-prepare-agentprepare-agentjacoco-instrumentinstrumentjacoco-restore-instrumented-classesrestore-instrumented-classesjacoco-reportpackagereport*


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