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