1. 程式人生 > 其它 >使用dropwizard(4)-加入測試-jacoco程式碼覆蓋率

使用dropwizard(4)-加入測試-jacoco程式碼覆蓋率

前言

dropwizard提供了一個簡單的測試框架。這裡簡單整合並加入jacoco測試。

Demo source

https://github.com/Ryan-Miao/l4dropwizard

本文是基於dropwizard入門之上的演進。

確保依賴都是最新的,或者自行解決版本衝突,比如jackson不同版本之間的類有所不同。

加入dropwizard-testing

在dependencies中增加依賴

<dependency>
    <groupId>io.dropwizard</groupId>
    <artifactId>dropwizard-testing</artifactId>
    <version>${dropwizard.version}</version>
    <scope>test</scope>
</dependency>

新增Mockito

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>2.12.0</version>
    <scope>test</scope>
</dependency>

新增jacoco

在properties下新增

<jacoco.skip.instrument>true</jacoco.skip.instrument>
<jacoco.percentage.instruction>0.01</jacoco.percentage.instruction>
<jacoco.percentage.branch>0</jacoco.percentage.branch>

在plugin新增

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>**/ioc/**/*</exclude>
            <exclude>**/exceptions/**/*</exclude>
            <exclude>**/connector/*</exclude>
            <exclude>**/valueobject/**/*</exclude>
            <exclude>**/exception/**/*</exclude>
            <exclude>**/entity/**/*</exclude>
            <exclude>**/constant/*</exclude>
            <exclude>**/*Test.*</exclude>
            <exclude>**/Dagger*</exclude>
            <exclude>**/*Factory.*</exclude>
            <exclude>**/*Module.*</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <id>jacoco-initialize</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-check</id>
            <phase>test</phase>
            <goals>
                <goal>check</goal>
            </goals>
            <configuration>
                <rules>
                    <rule implementation="org.jacoco.maven.RuleConfiguration">
                        <element>BUNDLE</element>
                        <limits>
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>INSTRUCTION</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>${jacoco.percentage.instruction}</minimum>
                            </limit>
                            <limit implementation="org.jacoco.report.check.Limit">
                                <counter>BRANCH</counter>
                                <value>COVEREDRATIO</value>
                                <minimum>${jacoco.percentage.branch}</minimum>
                            </limit>
                        </limits>
                    </rule>
                </rules>
            </configuration>
        </execution>
        <execution>
            <id>jacoco-report</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>jacoco-instrument</id>
            <phase>test</phase>
            <goals>
                <goal>instrument</goal>
            </goals>
            <configuration>
                <skip>${jacoco.skip.instrument}</skip>
            </configuration>
        </execution>
    </executions>
</plugin>

編寫測試

首先,更新依賴,

mvn clean install

IDEA中重新整理maven按鈕。

然後,新建Resource測試類:

package com.test.domain.resource;

import com.test.domain.entiry.GithubUser;
import com.test.domain.service.IGithubService;
import io.dropwizard.testing.junit.ResourceTestRule;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Created by Ryan Miao on 11/20/17.
 */
public class GithubResourceTest {

    private static final IGithubService service = mock(IGithubService.class);

    @ClassRule
    public static final ResourceTestRule resources = ResourceTestRule.builder()
            .addResource(new GithubResource(service))
            .build();


    @Before
    public void setup() {

    }

    @After
    public void tearDown(){
        // we have to reset the mock after each test because of the
        // @ClassRule, or use a @Rule as mentioned below.
        reset(service);
    }

    @Test
    public void testGetPerson() {

        GithubUser user = new GithubUser();
        String name = "Ryan";
        user.setName(name);
        when(service.getUserProfile(anyString())).thenReturn(user);
        GithubUser githubUser = resources.target("/github/users/ryan-miao").request().get(GithubUser.class);
        assertEquals(name, githubUser.getName());
        verify(service).getUserProfile("ryan-miao");
    }

}

驗收,檢視覆蓋率

mvn clean install

檢視jacoco覆蓋率

report在target/site/jacoco/index.html