반응형

설명

pom.xml 코드 수정

<profiles>
    <profile>
        <id>ci</id>
        <properties>
            <sonar.projectKey>EXAMPLE-PROJECT</sonar.projectKey>
            <sonar.host.url>http://sonarqube.example.com:9000</sonar.host.url>
            <sonar.login>c33b432f7c835c5beedc0f5847a7adcff01cdd7a</sonar.login>
        </properties>
    </profile>
</profiles>

<properties>
    ...
    <sonar.java.checkstyle.reportPaths>target/checkstyle-result.xml</sonar.java.checkstyle.reportPaths>
    ...
</properties>

<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jacoco</groupId>
    <artifactId>java-jacoco</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.9</version>
                <executions>
                    <execution>
                        <id>prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- 테스트 실패시에도 mvn 커맨드가 실패 처리되지 않도록 설정 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>

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

대상 코드 작성

public class Operator {
    public int add(int a, int b) {
        return a + b;
    }

    public double multiple(double a, double b) {
        return a * b;
    }
}

대상 코드 TC 작성

public class OperatorTest {
    private Operator operator = new Operator();

    @Test
    public void add() {
        int actual = operator.add(2, 6);
        int expected = 8;

        assertEquals(expected, actual);
    }
}

테스트 실행하기

mvn clean compile test

결과 확인하기 - index.html

  • <프로젝트경로>/target/site/jacoco/index.html

결과 확인하기 - jacoco.exec

  • IntelliJ > Analyze > Show Coverage Data
  • jacoco.exec 파일 추가 (경로 : <프로젝트>/target/jacoco.exec)
  • Show selected 버튼 클릭
반응형

+ Recent posts