为什么Jacoco报告由测试执行的行的0%覆盖率?

时间:2013-11-13 16:32:41

标签: java maven sonarqube cobertura jacoco

鉴于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%。

Sample.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));
    }
}

Sample_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>

0 个答案:

没有答案