Springboot2 - PowerMock - 单元测试

1.添加maven依赖


    org.powermock
    powermock-module-junit4
    2.0.0
    test


    org.powermock
    powermock-api-mockito2
    2.0.0
    test

2.单元测试公共基础类

纯PowerMock测试(不加载SpringContext) 

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;

/**
 * PowerMock测试基类
 *
 * @author luohq
 * @date 2018/6/20.
 */
@RunWith(PowerMockRunner.class)  //使用PowerMockRunner运行时
@PowerMockIgnore({"javax.management.*"}) //忽略一些mock异常
public class BasePowerMock {
}

PowerMock+Springboot测试(加载SpringContext) 

import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;


/**
 * PowerMock测试基类
 *
 * @author luohq
 * @date 2018/6/20.
 */
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringRunner.class)
@PowerMockIgnore({"javax.management.*"})//, "javax.net.ssl.*"
@SpringBootTest
public class BasePowerMock {

}

3.具体单元测试

纯PowerMock测试(不加载SpringContext) 

import com.mx.service.vehicle.parts.BasePowerMock;
import com.mx.service.vehicle.parts.model.vo.battery.BatteryPackPageVo;
import com.mx.service.vehicle.parts.service.BatteryPackService;
import com.mx.service.vehicle.parts.util.MxHttpRespUtility;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.test.util.ReflectionTestUtils;

import java.util.ArrayList;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

/**
 * 电池Controller - 单元测试
 *
 * @Ahthor luohq
 * @Date 2019-08-27
 */
public class BatteryControllerTest extends BasePowerMock {

    @Mock
    private BatteryPackService batteryPackService;

    @InjectMocks
    private BatteryController batteryController = new BatteryController();


    @Test
    public void getBatteryList() {

        /** 构造参数 */
        BatteryPackPageVo batteryPackPageVo = new BatteryPackPageVo();
        batteryPackPageVo.setPage(1);
        batteryPackPageVo.setRows(10);
        batteryPackPageVo.setOemId("300505");

        /** 录制mock操作 */
        when(batteryPackService.listPage(any()))
                .thenReturn(new ArrayList<>())
                .thenThrow(new RuntimeException("测试异常"));


        /** 执行操作 */
        //(1)执行mock查询操作
        Object result = batteryController.getBatteryList(batteryPackPageVo);
        assertTrue(MxHttpRespUtility.isSuccessResp(result));


        //(2)执行mock操作,抛出异常
        ReflectionTestUtils.setField(batteryController,  "batteryPackService", batteryPackService);
        result = batteryController.getBatteryList(batteryPackPageVo);
        assertTrue(!MxHttpRespUtility.isSuccessResp(result));
        verify(batteryPackService, times(2)).listPage(batteryPackPageVo);
    }
}

PowerMock+Springboot测试(加载SpringContext)  

import com.mx.service.vehicle.parts.BasePowerMock;
import com.mx.service.vehicle.parts.model.vo.battery.BatteryPackPageVo;
import com.mx.service.vehicle.parts.service.BatteryPackService;
import com.mx.service.vehicle.parts.util.MxHttpRespUtility;
import org.junit.Test;
import org.mockito.Mock;
import org.springframework.test.util.ReflectionTestUtils;

import javax.annotation.Resource;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;

/**
 * 电池Controller - 单元测试
 *
 * @Ahthor luohq
 * @Date 2019-08-27
 */
public class BatteryControllerTest2 extends BasePowerMock {


    @Mock
    private BatteryPackService batteryPackService;

    @Resource
    private BatteryController batteryController;


    @Test
    public void getBatteryList() {

        /** 构造参数 */
        BatteryPackPageVo batteryPackPageVo = new BatteryPackPageVo();
        batteryPackPageVo.setPage(1);
        batteryPackPageVo.setRows(10);
        batteryPackPageVo.setOemId("300505");

        /** 录制mock操作 */
        when(batteryPackService.listPage(any()))
                .thenThrow(new RuntimeException("测试异常"));


        /** 执行操作 */
        //(1)执行实际查询操作
        Object result = batteryController.getBatteryList(batteryPackPageVo);
        assertTrue(MxHttpRespUtility.isSuccessResp(result));


        //(2)执行mock操作,抛出异常
        ReflectionTestUtils.setField(batteryController,  "batteryPackService", batteryPackService);
        result = batteryController.getBatteryList(batteryPackPageVo);
        assertTrue(!MxHttpRespUtility.isSuccessResp(result));
        verify(batteryPackService, times(1)).listPage(batteryPackPageVo);
    }
}

4.生成cobertura测试覆盖率报告

maven插件


    dev_web_template
    
        
            org.springframework.boot
            spring-boot-maven-plugin
        

        
            org.apache.maven.plugins
            maven-compiler-plugin
           
           
           
           
            3.6.1
            
                1.8
                1.8
            

        

    


    
        
            org.codehaus.mojo
            cobertura-maven-plugin
            2.7
        

    

执行mvn cobertura:cobertura
在target目录下:target->site->cobertura->index.html
通过浏览器查看该index.html文件,即可得到统计结果:

Springboot2 - PowerMock - 单元测试_第1张图片

注:在使用cobertura进行springboot测试时,由于springboot项目中排除了对logback的依赖,引入了log4j框架,在执行cobertura命令时,springboot上下文环境一直启动不起来,报如下错误:

[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ dev-springboot-template ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.mx.server.dev.TestServiceTest
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/maven_reposity/org/apache/logging/log4j/log4j-slf4j-impl/2.11.2/log4j-slf4j-impl-2.11.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/maven_reposity/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
2019-08-27 19:21:57.737 ERROR [main][TestContextManager.java:250] - Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@7a8fa663] to prepare test instance [com.mx.server.dev.TestServiceTest@5ce33a58]
java.lang.IllegalStateException: Failed to load ApplicationContext
    ......
Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/D:/maven_reposity/org/apache/logging/log4j/log4j-slf4j-impl/2.11.2/log4j-slf4j-impl-2.11.2.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.apache.logging.slf4j.Log4jLoggerFactory
    ......
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.005 s <<< FAILURE! - in com.mx.server.dev.TestServiceTest
[ERROR] testSayHi(com.mx.server.dev.TestServiceTest)  Time elapsed: 0.001 s  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/D:/maven_reposity/org/apache/logging/log4j/log4j-slf4j-impl/2.11.2/log4j-slf4j-impl-2.11.2.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml: org.apache.logging.slf4j.Log4jLoggerFactory
 

问题分析:

虽然springboot依赖中已显式排除掉了对logback的依赖(如下),


   org.springframework.boot
   spring-boot-starter-web
   
       
      
         spring-boot-starter-logging
         org.springframework.boot
      
   


 

   org.springframework.boot
   spring-boot-starter-log4j2

但是plugin中cobertura-maven-plugin中引入了logback依赖(cobertura-maven-plugin->cobertura 2.1.1->logback);

解决方法:

在dependencyManagement中显式声明cobertura,并删除其依赖的logback-classic;


   
      
      
      
         net.sourceforge.cobertura
         cobertura
         2.1.1
         test
         
            
               ch.qos.logback
               logback-classic
            
         
      
   

5.多module统一报告

结合Ant,参考链接:http://www.thinkcode.se/blog/2012/02/18/test-coverage-in-a-multi-module-maven-project

你可能感兴趣的:(springboot)