Powermock and sonar Jacoco coverage incompatibility problem Resolution 3

Source: Internet
Author: User
Tags mongodb postgresql redis


The previous article describes how to let Surefire,failsafe and Jacoco maven plugin How to combine configuration, this article specifically explains how to use the Powermock agent to solve coverage incompatibility problems.
First add Powermock agent maven dependency in POM



<dependency>
  <groupId>org.powermock</groupId>
  <artifactId> Powermock-module-junit4-rule-agent</artifactid>
  <version>${powermock.version}</version>
  <scope>test</scope>
</dependency>


Note:it ' s recommended that's put powermock-module-junit4-rule-agent before JUnit in the classpath. (Remember to put this dependency before JUnit).



Next, modify the previous test code:
Implementation prior to modification



@RunWith (Powrmockrunner.class)
@PrepareForTest (x.class);
public class MyTest {
     private x x;
     @Before public
     void SetUp {
        x=powermockito.mock (x.class);
     }
     Tests goes here
     ...
}


The implementation after the modification
Remove Runwith, add @rule annotation, very simple.



@PrepareForTest (x.class);
public class MyTest {
     @Rule
     powermockrule rule = new Powermockrule ();

     private x x;
     @Before public
     void SetUp {
        x=powermockito.mock (x.class);
     }
     Tests goes here
     ...
}


Finally, it is said that in the mock final the agent startup parameters are added to the Surefire startup parameters, which we did not use, but added.



<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId> maven-surefire-plugin</artifactid>
                <version>2.15</version>
                <configuration>
                    <argLine>
                        -javaagent:${settings.localrepository}/org/powermock/powermock-module-javaagent/${ Powermock.version}/powermock-module-javaagent-${powermock.version}.jar-xx:-usesplitverifier ${surefireArgLine}
                    </argLine>                    <useSystemClassloader>true</useSystemClassloader>
                </configuration >
            </plugin>


In our project the last 3 ways to use the coverage of the difference, you can see that Cobertura still has the highest coverage, the use of Powermock agent after the coverage is also good.



Powermock&jacoco Powermock Agent&jacoco Powermock&cobertura
10% 50% 40g

References: https://github.com/jayway/powermock/wiki/PowerMockAgent

Alibaba Cloud Hot Products

Elastic Compute Service (ECS) Dedicated Host (DDH) ApsaraDB RDS for MySQL (RDS) ApsaraDB for PolarDB(PolarDB) AnalyticDB for PostgreSQL (ADB for PG)
AnalyticDB for MySQL(ADB for MySQL) Data Transmission Service (DTS) Server Load Balancer (SLB) Global Accelerator (GA) Cloud Enterprise Network (CEN)
Object Storage Service (OSS) Content Delivery Network (CDN) Short Message Service (SMS) Container Service for Kubernetes (ACK) Data Lake Analytics (DLA)

ApsaraDB for Redis (Redis)

ApsaraDB for MongoDB (MongoDB) NAT Gateway VPN Gateway Cloud Firewall
Anti-DDoS Web Application Firewall (WAF) Log Service DataWorks MaxCompute
Elastic MapReduce (EMR) Elasticsearch

Alibaba Cloud Free Trail

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.