相关文章推荐
贪玩的火车  ·  org.mockito.Answers.ge ...·  1 周前    · 
大鼻子的柿子  ·  超级简历WonderCV - 知乎·  1 年前    · 
暗恋学妹的消炎药  ·  An API ...·  1 年前    · 
忐忑的围巾  ·  Power ...·  1 年前    · 
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.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
 * PowerMock测试基类
 * @author luohq
 * @date 2018/6/20.
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@PowerMockIgnore({"javax.net.ssl.*", "javax.management.*", "javax.crypto.*"})
@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

1.添加maven依赖&lt;dependency&gt; &lt;groupId&gt;org.powermock&lt;/groupId&gt; &lt;artifactId&gt;powermock-module-junit4&lt;/artifactId&gt; &lt;version&gt;2.0.0&lt;/version&gt; &lt;sco... <groupId>org.scala-tools</groupId> <artifactId>maven-scala-plugin</artifactId> <version>2.15.2</version> <execut 项目环境:springmvc, spring, hibernate 事情是这样的,项目在启动时需要启动一个线程,执行扫描任务,因为不是例行性任务,所以不考虑quartz,要完成这个功能,可以有多种实现方法,比如: 1. 自定义监听器 public class StartupListener implements ServletContextListener { publ...
SQLErrorCodeSQLExceptionTranslator 是SQLExceptionTranslator 的默认实现。该实现使用指定数据库厂商的error code,比采用SQLState 更精确。转换过程基于一个JavaBean ( 类型为SQLErrorCodes ) 中的error code 。这个JavaBean 由SQLErrorCodesFactory 工厂类创建,其中的内容来自于“sql-error-codes.xml”配置
PowerMockito是一个用于Java单元测试的工具,它可以用来测试私有方法。使用PowerMockito的方法是在测试类上使用@PowerMock注解,并在测试方法中使用PowerMockito.spy()来创建被测试类的spy对象,然后使用PowerMockito.when()来指定私有方法的返回值或执行其他操作。 @RunWith(PowerMockRunner.class) @PrepareForTest(MyClass.class) public class MyClassTest { @Test public void testPrivateMethod() throws Exception { MyClass myClass = PowerMockito.spy(new MyClass()); PowerMockito.when(myClass, "privateMethod").thenReturn("Hello PowerMockito"); String result = myClass.publicMethod(); assertEquals("Hello PowerMockito", result); 请注意,使用PowerMockito进行单元测试可能会使测试变得复杂和难以维护,因此在使用PowerMockito之前需要谨慎考虑。