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