给定 Sample.java,一个测试类 Sample_UT.java,SonarQube 3.7.3,为什么 Jacoco 会报告 Sample.java 的 0% 覆盖率而 Cobertura 报告 100%?该行return new RawResponse(builder.delete(String.class, requestBody));
已明确执行,但 Jacoco 报告说它没有执行。我的 jacoco 和 cobertura maven 配置文件如下所示。Jacoco 在同一包中的另一个类上报告了 100%。
示例.java:
package com.example;
import com.sun.jersey.api.client.WebResource;
public class Sample {
protected RawResponse issueQuery(WebResource.Builder builder, DataTransferObject requestBody) {
return new RawResponse(builder.delete(String.class, requestBody));
}
}
示例_UT.java:
package com.example;
import com.sun.jersey.api.client.WebResource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Sample.class, WebResource.Builder.class})
public class Sample_UT {
@Mock
private WebResource.Builder mockBuilder;
@Mock
private WebResource mockWebResource;
@Mock
private DataTransferObject mockTestRequestBody;
private String testResponseBody = "response";
@InjectMocks
private Sample subject;
@Test
public void testThat_IssueQuery_ReturnsRawResponse_FromBuilder() throws Exception {
when(mockBuilder.delete(String.class, mockTestRequestBody)).thenReturn(testResponseBody);
assertEquals(testResponseBody, subject.issueQuery(mockBuilder, mockTestRequestBody).getBody());
}
}
来自 settings.xml 的片段:
<profile>
<id>jacoco</id>
<properties>
<sonar.branch>JAC</sonar.branch>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.jacoco.reportPath>target/jacoco.report</sonar.jacoco.reportPath>
<sonar.dynamicAnalysis>true</sonar.dynamicAnalysis>
<sonar.junit.reportsPath>target/surefire-reports</sonar.junit.reportsPath>
<forceAnaylysis>true</forceAnaylysis>
</properties>
</profile>
<profile>
<id>cobertura</id>
<properties>
<sonar.branch>COB</sonar.branch>
<sonar.java.coveragePlugin>cobertura</sonar.java.coveragePlugin>
<sonar.dynamicAnalysis>true</sonar.dynamicAnalysis>
<sonar.junit.reportsPath>target/surefire-reports</sonar.junit.reportsPath>
<forceAnaylysis>true</forceAnaylysis>
</properties>
</profile>