我正在使用Maven多项目构建。我正在使用Powermock来模拟用于编写单元测试用例的类。因此,为了获得jacoco覆盖,我使用Jacoco离线仪器,并设置sonar.dynamicAnalysis = reuseReports来获得声纳覆盖范围。当我尝试这样做时,我收到来自Jacoco和Sonar的错误:
[INFO] Analysing C:\Program Files (x86)\Jenkins\workspace\Test\target\jacoco.exec
[WARNING] Exception during analysis of file C:\Program Files (x86)\Jenkins\workspace\Test\target\classes\cache\TestClass.class
java.io.IOException: Error while analyzing
C:\Program Files (x86)\Jenkins\workspace\Test\target\classes\cache\TestClass.class
at org.jacoco.core.analysis.Analyzer.analyzerError(Analyzer.java:155)
at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:130)
at org.jacoco.core.analysis.Analyzer.analyzeClass(Analyzer.java:147)
at org.sonar.plugins.jacoco.JacocoReportReader.analyzeClassFile(JacocoReportReader.java:139)
at org.sonar.plugins.jacoco.JacocoReportReader.analyzeFiles(JacocoReportReader.java:114)
at org.sonar.plugins.jacoco.AbstractAnalyzer.readExecutionData(AbstractAnalyzer.java:133)
at org.sonar.plugins.jacoco.AbstractAnalyzer.analyse(AbstractAnalyzer.java:102)
at org.sonar.plugins.jacoco.JaCoCoSensor.execute(JaCoCoSensor.java:87)
at org.sonar.scanner.sensor.SensorWrapper.analyse(SensorWrapper.java:53)
at org.sonar.scanner.phases.SensorsExecutor.executeSensor(SensorsExecutor.java:88)
at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:82)
at org.sonar.scanner.phases.SensorsExecutor.execute(SensorsExecutor.java:68)
at org.sonar.scanner.phases.AbstractPhaseExecutor.execute(AbstractPhaseExecutor.java:88)
at org.sonar.scanner.scan.ModuleScanContainer.doAfterStart(ModuleScanContainer.java:180)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:144)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:129)
at org.sonar.scanner.scan.ProjectScanContainer.scan(ProjectScanContainer.java:286)
at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:281)
at org.sonar.scanner.scan.ProjectScanContainer.scanRecursively(ProjectScanContainer.java:279)
at org.sonar.scanner.scan.ProjectScanContainer.doAfterStart(ProjectScanContainer.java:259)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:144)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:129)
at org.sonar.scanner.task.ScanTask.execute(ScanTask.java:48)
at org.sonar.scanner.task.TaskContainer.doAfterStart(TaskContainer.java:84)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:144)
at org.sonar.core.platform.ComponentContainer.execute(ComponentContainer.java:129)
at org.sonar.scanner.bootstrap.GlobalContainer.executeTask(GlobalContainer.java:121)
at org.sonar.batch.bootstrapper.Batch.doExecuteTask(Batch.java:118)
at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:72)
at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
at com.sun.proxy.$Proxy19.execute(Unknown Source)
at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:171)
at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:128)
at org.sonarsource.scanner.maven.bootstrap.ScannerBootstrapper.execute(ScannerBootstrapper.java:63)
at org.sonarsource.scanner.maven.SonarQubeMojo.execute(SonarQubeMojo.java:108)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:347)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:154)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:582)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
Caused by: java.lang.IllegalStateException:cache\TestClass is already instrumented.
at org.jacoco.core.internal.instr.InstrSupport.assertNotInstrumented(InstrSupport.java:176)
我不确定为什么jacoco会再次试图获得声纳覆盖率。我暂时坚持这个问题。任何帮助都会很棒。
答案 0 :(得分:0)
根据http://www.jacoco.org/jacoco/trunk/doc/instrument-mojo.html:
执行离线检测。 请注意,执行测试后,您必须在"restore-instrumented-classes" goal的帮助下恢复原始类。
异常消息already instrumented
具有误导性,但在尝试分析已检测的类而不是原始类时会产生此消息。
答案 1 :(得分:0)
我不确定您的情况如何,但是我已经成功使用powermock-module-javaagent
通过Jacoco生成了PowerMock报道。
只需确保将Powermock代理放在 jacoco代理之后:
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>true</useSystemClassLoader>
<argLine>${jacocoArgLine} -javaagent:${settings.localRepository}/org/powermock/powermock-module-javaagent/${powermock.version}/powermock-module-javaagent-${powermock.version}.jar -noverify</argLine>
...
如果要查看示例,请查看以下项目:https://github.com/jfcorugedo/sonar-scanner
在这里您可以看到声纳将静态方法和PowerMock模拟的new
语句考虑在内:
如果要模拟new
语句,请确保使用PowerMockRule
而不是PowerMockRunner
。