2

I'm trying to use JaCoCo API with CoreTutorial.

It provides how to check coverage with instrumented classes.

public void execute() throws Exception {
        final String targetName = TestTarget.class.getName();

        // For instrumentation and runtime we need a IRuntime instance
        // to collect execution data:
        final IRuntime runtime = new LoggerRuntime();

        // The Instrumenter creates a modified version of our test target class
        // that contains additional probes for execution data recording:
        final Instrumenter instr = new Instrumenter(runtime);
        final byte[] instrumented = instr.instrument(getTargetClass(targetName),
                targetName);

        // Now we're ready to run our instrumented class and need to startup the
        // runtime first:
        final RuntimeData data = new RuntimeData();
        runtime.startup(data);

        // In this tutorial we use a special class loader to directly load the
        // instrumented class definition from a byte[] instances.
        final MemoryClassLoader memoryClassLoader = new MemoryClassLoader();
        memoryClassLoader.addDefinition(targetName, instrumented);
        final Class<?> targetClass = memoryClassLoader.loadClass(targetName);

        // Here we execute our test target class through its Runnable interface:
        final Runnable targetInstance = (Runnable) targetClass.newInstance();
        targetInstance.run();

        // At the end of test execution we collect execution data and shutdown
        // the runtime:
        final ExecutionDataStore executionData = new ExecutionDataStore();
        final SessionInfoStore sessionInfos = new SessionInfoStore();
        data.collect(executionData, sessionInfos, false);
        runtime.shutdown();

        // Together with the original class definition we can calculate coverage
        // information:
        final CoverageBuilder coverageBuilder = new CoverageBuilder();
        final Analyzer analyzer = new Analyzer(executionData, coverageBuilder);
        analyzer.analyzeClass(getTargetClass(targetName), targetName);

        // Let's dump some metrics and line coverage information:
        for (final IClassCoverage cc : coverageBuilder.getClasses()) {
            out.printf("Coverage of class %s%n", cc.getName());

            printCounter("instructions", cc.getInstructionCounter());
            printCounter("branches", cc.getBranchCounter());
            printCounter("lines", cc.getLineCounter());
            printCounter("methods", cc.getMethodCounter());
            printCounter("complexity", cc.getComplexityCounter());

            for (int i = cc.getFirstLine(); i <= cc.getLastLine(); i++) {
                out.printf("Line %s: %s%n", Integer.valueOf(i),
                        getColor(cc.getLine(i).getStatus()));
            }
        }
    }

I want to check coverage of classes when the specific tests run. That test case uses the classes that I want to check coverage, but problem is those classes hasn't be instrumented.

public class AccountTest {

    Application a;
    Account account;

    @Before
    public void setUp() {
        a = new Application(); // the class Application hasn't be instrumented. 
        account = new Account(); // I want to check coverage of Application, Account classes
                                // when this test runs.

    }

    @Test
    public void testUpdate() {
        a.account.update(100);
        MyAssert.assertTrue(a.account.balance == 100);
        a.account.DAILY_LIMIT = -1000;
        if (Configuration.logging && Configuration.dailylimit) {
            assertFalse((a.account.withdraw + 100) < a.account.DAILY_LIMIT);
        }

    }
}

How can I get the coverage data of specific classes without modifying any test case classes?

5ubin
  • 21
  • 1
  • Well, you don't have any code to instrument the classes you want to be instrumented. Also, I don't see the code that would inoke the test, but the tests must be ran with context class loader to be the memory classloader you created. – Pawel Veselov Jan 31 '21 at 07:32

0 Answers0