Springboot2 - PowerMock - 单元测试

1.添加maven依赖

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>2.0.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito2</artifactId>
    <version>2.0.0</version>
    <scope>test</scope>
</dependency>

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

<build>
    <finalName>dev_web_template</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <!-- 默认3.8,回退到3.6.1,否则报异常: -->
            <!-- Unable to instrument file .../someClass.class -->
            <!-- java.lang.RuntimeException -->
            <!-- at org.objectweb.asm.MethodVisitor.visitParameter(Unknown Source) -->
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

    </plugins>

</build>

<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.7</version>
        </plugin>

    </plugins>
</reporting>

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

注:在使用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的依赖(如下),

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   <exclusions>
       <!--删除logback配置-->
      <exclusion>
         <artifactId>spring-boot-starter-logging</artifactId>
         <groupId>org.springframework.boot</groupId>
      </exclusion>
   </exclusions>
</dependency>

 <!--集成log4j2-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

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

解决方法:

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

<dependencyManagement>
   <dependencies>
      <!-- 删除依赖cobertura-maven-plugin->cobertura 2.1.1->logback -->
      <!-- 解决执行cobertura:cobertura 日志加载失败(logback, log4j冲突)问题 -->
      <dependency>
         <groupId>net.sourceforge.cobertura</groupId>
         <artifactId>cobertura</artifactId>
         <version>2.1.1</version>
         <scope>test</scope>
         <exclusions>
            <exclusion>
               <groupId>ch.qos.logback</groupId>
               <artifactId>logback-classic</artifactId>
            </exclusion>
         </exclusions>
      </dependency>
   </dependencies>
</dependencyManagement>

5.多module统一报告

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

发布了56 篇原创文章 · 获赞 6 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/luo15242208310/article/details/100098274