Jacoco generates coverage report for only one test class

So let's say I have a test

@Test
public void testA(){
    new A().doSomthing();
}

And let's say it covers a method doSomething(). Now I have 10 million tests in my project, and this test is just one of them. A little test does not do much.

Now let's say that my method is doSomethingas follows: -

public void doSomething() {
    if (var1)
        killMylSelf();
    else if (var2)
        killMyMother();
    else
        killMySelfAndMyMother();
}

So you can see that there are many branches in the method, which therefore call other methods that have even more branches. When I run testA, I want to see which branches I skipped in the code that was executed, how I can achieve this WITHOUT USING ALL HISTORICAL TESTS AND ONLY TESTING WHICH I APPLY,

, ,

+4
1

JaCoCo , , . , , , , , , .

Maven , maven-surefire-plugin, option test . :

src/main/java/Example.java:

public class Example {
  public void doSomething(int p) {
    if (p == 1) {
      a();
    } else {
      b();
    }
  }

  private void a() {
    System.out.println("a");
  }

  private void b() {
    System.out.println("b");
  }
}

src/test/java/ExampleTest.java:

import org.junit.Test;

public class ExampleTest {
  @Test
  public void test1() {
    new Example().doSomething(1);
  }

  @Test
  public void test2() {
    new Example().doSomething(2);
  }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>example</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20.1</version>
      </plugin>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.9</version>
        <executions>
          <execution>
            <id>default-prepare-agent</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>default-report</id>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

mvn clean verify -Dtest=ExampleTest#test1 target/site/jacoco:

test1 coverage

mvn clean verify -Dtest=ExampleTest#test2 :

test2 coating

test1 test2 .

- mvn clean verify :

coverage of all tests

clean: target/jacoco.exec (. destfile jacoco-maven-plugin). JaCoCo (. append jacoco-maven-plugin), clean, .

Gradle, - build.gradle:

apply plugin: 'java'
apply plugin: 'jacoco'

repositories {
  mavenCentral()
}

dependencies {
  testCompile 'junit:junit:4.12'
}

gradle clean test --tests ExampleTest.test1 jacocoTestReport , test1, , Maven.

Maven clean, build/jacoco/test.exec - . append JaCoCo Gradle Plugin.

Eclipse IDE, EclEmma Eclipse, JaCoCo Eclipse IDE "Eclipse IDE Java", Oxygen (4.7). Eclipse IDE - , , "Coverage As → JUnit Test".

+2

Source: https://habr.com/ru/post/1690682/


All Articles