Code Coverage Report with Jacoco for IntegrationTests runs on Weblogic server

Unknown

I have written unit test for the different domain classes and service classes (Webservice). Now I want to use JoCoCo for generating code coverage report. So the idea is to know the code coverage on my code running on weblogic when we do some action like manual usage of the site or like launching Junit test. I have deployed ear file in my weblogic server. How do I connect JoCoCo to weblogic server? Could you please any one tell me how to configure the JaCoCo with weblogic server and to generate the report.

Unknown

If you need to get the code coverage for tests against Java code, then JaCoCo is a very good option, because you don’t need to instrument the code before hand, it will do that on-the-fly during the tests.

Attach JaCoCo to Weblogic JVM :

The JaCoCo collector attaches it self as an agent to the JVM and collects code coverage tools continuously. We’ll need to add JVM parameters to set this up.

Step1: To do this edit the setDomainEnv.cmd or setDomainEnv.sh file to add the following parameter:

set JACOCO_PROPERTIES=-javaagent:c:/tools/jacoco-0.7.1-20140326.062324-3/lib/jacocoagent.jar=destfile=c:/temp/jacoco.exec,output=file,address=,includes=com.package

In the same file add this to the EXTRA_JAVA_PROPERTIES environment variable:

set
EXTRA_JAVA_PROPERTIES=-Dcommon.components.home=%COMMON_COMPONENTS_HOME%
-Djrf.version=11.1.1 -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
-Ddomain.home=%DOMAIN_HOME% -Djrockit.optfile=%COMMON_COMPONENTS_HOME%\modules\oracle.jrf_11.1.1\jrocket_optfile.txt
-Doracle.server.config.dir=%ORACLE_DOMAIN_CONFIG_DIR%\servers\%SERVER_NAME%
-Doracle.domain.config.dir=%ORACLE_DOMAIN_CONFIG_DIR% -Digf.arisidbeans.carmlloc=%ORACLE_DOMAIN_CONFIG_DIR%\carml -Digf.arisidstack.home=%ORACLE_DOMAIN_CONFIG_DIR%\arisidprovider -Doracle.security.jps.config=%DOMAIN_HOME%\config\fmwconfig\jps-config.xml
-Doracle.deployed.app.dir=%DOMAIN_HOME%\servers\%SERVER_NAME%\tmp_WL_user
-Doracle.deployed.app.ext=- -Dweblogic.alternateTypesDirectory=%ALT_TYPES_DIR% -Djava.protocol.handler.pkgs=%PROTOCOL_HANDLERS% %WLS_JDBC_REMOTE_ENABLED% %**JACOCO_PROPERTIES**%
%EXTRA_JAVA_PROPERTIES%

Step2: Restart the Weblogic Server Because JaCoCo attaches itself to the JVM, you’ll need to restart the Weblogic server. After starting the JVM again with startWeblogic.sh or startWeblogic.cmd, you’ll see that the file location listed destfile=c:/temp/jacoco.exec will be created. Initially it will be empty, but on shutdown of the JVM it will be filled.

Step3: Test your application Once the JaCoCo agent is attached, deploy your application and run your integration tests. JaCoCo will instrument the code on-the-fly and collect instruction-level and branch-level coverage information.

Step4: Generate your report

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jacoco.core.analysis.Analyzer;
import org.jacoco.core.analysis.CoverageBuilder;
import org.jacoco.core.analysis.IBundleCoverage;
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.tools.ExecFileLoader;
import org.jacoco.report.DirectorySourceFileLocator;
import org.jacoco.report.FileMultiReportOutput;
import org.jacoco.report.IReportVisitor;
import org.jacoco.report.html.HTMLFormatter;

public class JaCoCoReportGenerator{
    private static String title = "Code Coverage";

    private static File executionDataFile = new File("your path where you have jacoco.exec");
    private static List<File> classesDirectory;
    private static List<File> sourceDirectory;
    private static File reportDirectory = new File("path where you want your report to be generated");
    private ExecFileLoader execFileLoader;


    public JaCoCoReportGenerator(){
        super();
        initializeClassFileList();
        initializeSourceFileList();
    }

    private void initializeClassFileList(){
        classesDirectory = new ArrayList<File>();
        File file0 = new File("class file path");
        classesDirectory.add(file0);
    }

    private void initializeSourceFileList(){
        sourceDirectory = new ArrayList<File>();
        File file0 = new File("source file path");
        sourceDirectory.add(file0);

    }

    public void create() throws IOException{

        // Read the jacoco.exec file. Multiple data files could be merged
        // at this point
        loadExecutionData();

        // Run the structure analyzer on a single class folder to build up
        // the coverage model. The process would be similar if your classes
        // were in a jar file. Typically you would create a bundle for each
        // class folder and each jar you want in your report. If you have
        // more than one bundle you will need to add a grouping node to your
        // report
        IBundleCoverage bundleCoverage = analyzeStructure();

        createReport(bundleCoverage);

    }

    private void createReport(IBundleCoverage bundleCoverage) throws IOException{

        // Create a concrete report visitor based on some supplied
        // configuration. In this case we use the defaults
        HTMLFormatter htmlFormatter = new HTMLFormatter();
        IReportVisitor visitor = htmlFormatter.createVisitor(new FileMultiReportOutput(reportDirectory));

        // Initialize the report with all of the execution and session
        // information. At this point the report doesn't know about the
        // structure of the report being created
        visitor.visitInfo(execFileLoader.getSessionInfoStore().getInfos(),execFileLoader.getExecutionDataStore().getContents());

        for (File sourceFile : sourceDirectory) {
            visitor.visitBundle(bundleCoverage,new DirectorySourceFileLocator(sourceFile,POSMConstant.ENCODING,4));
        }
        // Populate the report structure with the bundle coverage information.
        // Call visitGroup if you need groups in your report.

        // Signal end of structure information to allow report to write all
        // information out
        visitor.visitEnd();

    }


    private void loadExecutionData() throws IOException{
        execFileLoader = new ExecFileLoader();
        execFileLoader.load(executionDataFile);// Loading my jacoco.exe file
        analyzeStructure();
    }

    private IBundleCoverage analyzeStructure() throws IOException{
        final CoverageBuilder coverageBuilder = new CoverageBuilder();
        final Analyzer analyzer = new Analyzer(execFileLoader.getExecutionDataStore(),coverageBuilder);
        for (File classFile : classesDirectory) {
            analyzer.analyzeAll(classFile);// Analyzes all class files contained in the given file or folder. Folders are searched recursively.
        }
        return coverageBuilder.getBundle(title);
    }



    public static void main(String[] args) throws IOException{
        JaCoCoReportGenerator generator = new JaCoCoReportGenerator();
        generator.create();
    }

    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

generating jacoco code coverage report for all sub modules

From Dev

Jacoco Test coverage report shows 0%

From Dev

Sonar maven jacoco code coverage for Multimodule project

From Dev

maven jacoco: not generating code coverage report

From Dev

Jacoco coverage of unit test code

From Dev

Jacoco Code Coverage in android studio

From Dev

JaCoCo coverage report setups(exclude test classes)

From Dev

Why might Jacoco be showing a code coverage of 0?

From Dev

How to check code coverage with JaCoCo agent?

From Dev

Exclude folder in jacoco coverage report

From Dev

Gradle - Jacoco code coverage without running connectedCheck

From Dev

Code coverage with jacoco for a Android library

From Dev

Jacoco code coverage is affected by AspectJ

From Dev

Jacoco Coverage and Report Task with Ant

From Dev

Minimum code coverage threshold in Jacoco Gradle

From Dev

maven jacoco plugin does not generate coverage report

From Dev

Missing Jacoco Code Coverage and IncompatibleClassChangeError

From Dev

Android - Jacoco code coverage ignores Robolectric tests

From Dev

Jacoco 0% Code Coverage

From Dev

How to report Jacoco Groovy code coverage to Sonar using new Gradle SonarQube plugin?

From Dev

Jacoco: Find code coverage for external tests

From Dev

How to exclude a line from jacoco code coverage?

From Dev

Android - Jacoco code coverage ignores Robolectric tests

From Dev

Jacoco Test coverage report shows 0%

From Dev

java code coverage with jacoco

From Dev

JaCoCo Debug Coverage Test Report

From Dev

JaCoCo coverage report setups(exclude test classes)

From Dev

Jacoco 0% Code Coverage

From Dev

How would I add an annotation to exclude a method from a jacoco code coverage report?

Related Related

HotTag

Archive