天天看點

sonar掃描重複率忽略檔案、單元測試覆寫率

單元測試Demo

以金融科技項目技術棧為例

對Controller -> Service ->(Feign) -> Mapper層聯合測試

1.業務類 WaybillFinancingController.java
@RestController
public class WaybillFinancingController {
    @Autowired
    private WaybillFinancingService waybillFinancingService;
​
    @PostMapping(value = UrlMapping.YF_RZ_GET_ASSET_POOL_LIST)
    public ResultDTO<List<AssetPool>> getAssetPoolList(@RequestBody AssetPool assetPool){
        ResultDTO<List<AssetPool>> resultDTO = new ResultDTO<>();
        try {
            resultDTO = waybillFinancingService.getAssetPoolList(assetPool);
            if(!resultDTO.getSuccess()){
                throw new RzServiceException(resultDTO.getMessage());
            }
        } catch (RzServiceException e){
            log.error(e.getMessage(),e);
            resultDTO.setSuccess(false);
            resultDTO.setMessage(e.getMessage());
        }
        return resultDTO;
    }
​
}      
2.業務類 WaybillFinancingServiceImpl.java
@Service
public class WaybillFinancingServiceImpl{
public ResultDTO<List<AssetPool>> getAssetPoolList(AssetPool assetPool) {
        ResultDTO<List<AssetPool>> resultDTO = new ResultDTO<>();
        try {
            OperatorDTO operator = UserUtils.getOperator();
            assetPool.setCustomerId(operator.getCustomerId());
            assetPool.setStatus("1"); 
            assetPool.setPoolType("10");
            resultDTO = assetsService.getAssetPoolList(assetPool);
            if(!resultDTO.getSuccess()){
                throw new RzServiceException(resultDTO.getMessage());
            }
        } catch (RzServiceException e) {
            resultDTO.setSuccess(false);
            resultDTO.setMessage(e.getMessage());
        }
        return resultDTO;
    }
public List<RzFtbApplyDtlInfo> getFinanceApplicationByParam(RzFtbApplyDtlInfo rzFtbApplyDtlInfo) {
        Map<String, String> param = new HashMap<>();
        param.put("billId", rzFtbApplyDtlInfo.getBillId());
        List<RzFtbApplyDtlInfo> rzFtbApplyDtlInfoList = rzFtbApplyDtlInfoMapper.selectByMap(param);
        return rzFtbApplyDtlInfoList;
    }
}      
3.業務類 RpcAssetsService.java
@FeignClient(value = "rf-assets",configuration = FeignRequestInterceptor.class, primary = false)
public interface RpcAssetsService {
    @PostMapping(value = "/a/assetPool/list")
    public ResultDTO<List<AssetPool>> getAssetPoolList(AssetPool assetPool);
}      
4.業務類 RzFtbApplyDtlInfoMapper.java
@Mapper
public interface RzFtbApplyDtlInfoMapper {
    List<RzFtbApplyDtlInfo> selectByMap(Map map);
}      
5.測試類 WaybillFinancingControllerTest.java
​
@Slf4j
@SpringBootTest(classes = App.class)
@SpringBootApplication(scanBasePackages = "com.mountslink")
@TestPropertySource(properties = {"RUNPROFILE=dev", "feign.test.mock=true"})
public class WaybillFinancingControllerTest {
    @Resource
    MockHttpServletRequest request;
    @Resource
    WaybillFinancingController waybillFinancingController;
    @BeforeEach
    void init() {}
    @Test
    public void getAssetPoolList() {
        String customerName = UUID.randomUUID().toString().replaceAll("-", "");
        log.info("customerName:{}", customerName);
        AssetPool assetPool = new AssetPool();
        assetPool.setCustomerName(customerName);
        ResultDTO<List<AssetPool>> resultDTO = waybillFinancingController.getAssetPoolList(assetPool);
        log.info(resultDTO.getData().get(0).getCustomerName());
        assumeTrue(resultDTO.getSuccess());
        assertEquals(resultDTO.getData().get(0).getCustomerName(), customerName);
    }
    @Test
    public void getFinanceApplicationByParam(){
        RzFtbApplyDtlInfo param=new RzFtbApplyDtlInfo();
        param.setBillId("S2021082764125");
        ResultDTO<List<RzFtbApplyDtlInfo>> resultDTO= waybillFinancingController.getFinanceApplicationByParam(param);
        assumeTrue(resultDTO.getSuccess());
        assertEquals(resultDTO.getData().get(0).getPayerName(), "上海潤益網際網路科技股份有限公司");
    }
}
​      
6.測試類 FeignMock需要實作業務FeignClientService
@Slf4j
@Primary
@Component
@ConditionalOnProperty(name = "feign.test.mock", havingValue = "true")
public class RpcAssetsServiceFeignClientMock implements RpcAssetsService {
    @Override
    public ResultDTO<List<AssetPool>> getAssetPoolList(AssetPool assetPool) {
        log.debug("FeignClientMock start--------------getAssetPoolList");
        ResultDTO resultDTO = new ResultDTO<List<AssetPool>>();
        resultDTO.setData(Lists.newArrayList(assetPool));
        resultDTO.setSuccess(true);
        return resultDTO;
    }
}      

單元測試代碼生成插件Squaretest

使用指南

1.安裝插件
2.插件介紹
3.生成測試類

4.單元測試覆寫率

1.pom.xml增加plugin

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.7</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
    </plugin>