单元测试监控

1、配置sonar的url 

<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>

2、配置插件 

<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>

3、生成数据推送到服务器上 

#项目生成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

4、不要用SpringBootTest

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

}

5、使用

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

}

6、PrepareForTest 使用说明

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, "我成年了");

    }
}

转载于:https://my.oschina.net/u/3777515/blog/3077943

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值