單元測試監控

一、配置sonar的url java

<properties>
    <sonar.host.url>http://sonar.puhuitech.cn</sonar.host.url>
    <sonar.language>java</sonar.language>
    <sonar.exclusions>
        src/main/java/com/iqianjin/sms/service/ext/ytxYy/**,
        src/main/java/com/iqianjin/sms/service/smsSendCountStatisticsServiceImpl/**,
        src/main/java/com/iqianjin/sms/dto/**
    </sonar.exclusions>
</properties>

二、配置插件 spring

<build>
        <finalName>${project.artifactId}</finalName>
        <defaultGoal>package</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <attach>true</attach>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.7.201606060606</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

三、生成數據推送到服務器上 apache

#項目生成jacoco.exec文件
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent package -Pxxx -Dmaven.test.failure.ignore=true

#上傳測試覆蓋率數據
mvn sonar:sonar -Dsonar.projectKey=pub-moon-service -Dsonar.projectName=pub-moon-service

四、不要用SpringBootTest服務器

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore({"javax.net.*","javax.management.*", "javax.security.*", "javax.crypto.*", "org.mockito.*"})
@SpringBootTest
public class BaseJUnitTest {

}

五、使用maven

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.net.*","javax.management.*", "javax.security.*", "javax.crypto.*", "org.mockito.*"})
public class BaseJUnitTest {

}

六、PrepareForTest 使用說明spring-boot

package com..coupon.service.service;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;

/**
 * 當須要mock final方法的時候註解,@PrepareForTest裏寫的類是final方法所在的類。
 * 當須要mock 靜態方法的時候,註解@PrepareForTest裏寫的類是靜態方法所在的類。
 * 當須要mock 私有方法的時候,註解@PrepareForTest裏寫的類是私有方法所在的類。
 * 當須要mock系統類的靜態方法的時候,註解裏@PrepareForTest寫的類是須要調用系統方法所在的類。
 */
@RunWith(PowerMockRunner.class)
//@PrepareForTest({TestService.class})
public class TestServiceTest {

    @InjectMocks
    private TestService testService;

    @Mock
    private TestManager testManager;

    @Test
    public void testAddName() {
        when(testManager.getDoubleAge(anyInt())).thenReturn(0);
        String name0 = testService.getName(0);
        Assert.assertEquals(name0, "我剛出生");

        when(testManager.getDoubleAge(anyInt())).thenReturn(10);
        String name10 = testService.getName(0);
        Assert.assertEquals(name10, "我未成年");

        when(testManager.getDoubleAge(anyInt())).thenReturn(20);
        String name20 = testService.getName(0);
        Assert.assertEquals(name20, "我成年了");

    }
}
相關文章
相關標籤/搜索