我是靠谱客的博主 干净小白菜,最近开发中收集的这篇文章主要介绍PowerMockito的基本使用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

PowerMockito经常会结合Mockito使用,先说一下这2个的介绍:

1.Mockito和PowerMockito的简介


Mockito和PowerMockito是什么东西呢?他们有什么作用呢?Mocktio和PowerMockito都是Mock的工具类,主要是Java的类库,Mock就是伪装的意思。他们适用于单元测试中,对于单元测试来说,我们不希望依赖于第三方的组件,比如数据库、Webservice等。在写单元测试的时候,我们如果遇到了这些需要依赖第三方的情况,我们可以使用Mock的技术,伪造出来我们自己想要的结果。对于Java而言,mock的对象主要是Java 方法和 Java类。

下面我就介绍一下怎么使用Mockito和PowerMockito去进行Mock。

2.Mockito和PowerMockito的区别


在我看来,PowerMockito是Mockito的一种增强,他们的PowerMockito可以调用Mockito的方法,但是对于Mocktio不能Mock的对象或者方法,我们可以使用PowerMockito来实现。比如Mockito不能用于static Method, final method, 枚举类, private method,这些我们都可以用PowerMockito来实现,当PowerMockito和mockito结合使用的时候,我们需要考虑兼容性的问题。两者的版本需要兼容。

 

MockitoPowerMockito
2.8.9+2.x
2.8.0-2.8.91.7.x
2.7.51.7.0RC4
2.4.01.7.0RC2
2.0.0-beta - 2.0.42-beta1.6.5-1.7.0RC
1.10.8 - 1.10.x1.6.2 - 2.0
1.9.5-rc1 - 1.9.51.5.0 - 1.5.6
1.9.0-rc1 & 1.9.01.4.10 - 1.4.12
1.8.51.3.9 - 1.4.9
1.8.41.3.7 & 1.3.8
1.8.31.3.6
1.8.1 & 1.8.21.3.5
1.81.3
1.71.2.5

Ref:https://github.com/powermock/powermock/wiki/Mockito

3.具体用法

本文实现实现需要构造的接口和需要返回值的接口

  • 引入依赖
<dependencies>
	<dependency>
		<groupId>org.projectlombok</groupId>
		<artifactId>lombok</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context-support</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-test</artifactId>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.12</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.mockito</groupId>
		<artifactId>mockito-all</artifactId>
		<version>2.0.2-beta</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.powermock</groupId>
		<artifactId>powermock-module-junit4</artifactId>
		<version>1.7.4</version>
		<scope>test</scope>
	</dependency>
	<dependency>
		<groupId>org.powermock</groupId>
		<artifactId>powermock-api-mockito</artifactId>
		<version>1.7.4</version>
		<scope>test</scope>
	</dependency>
</dependencies>
  • 需要Mock的类:

ProcessDB.java

package com.github.mock.simple.vo;

public class ProcessDB {
    
    public ProcessDB(String ss){
        System.out.println(ss + " Enter ProcessDB ...");
    }
    
    public ProcessDB(){
        System.out.println("Enter ProcessDB ...");
    }
    
    public void getResultOfConnectDBNoReturn(String ss) {
        System.out.println(ss + " Enter getResultOfConnectDBNoReturn ...");
    }
    
    public String getResultOfConnectDB() {
        return "haha, Really went to the database";
    }
}
 
  • 需要测试的类:

IUserService.java

package com.github.mock.simple.user;

public interface IUserService {

    public String testedMehtod();

}

UserServiceImpl.java

package com.github.mock.simple.user.impl;

import org.springframework.stereotype.Service;

import com.github.mock.simple.user.IUserService;
import com.github.mock.simple.vo.ProcessDB;

@Service
public class UserServiceImpl implements IUserService {

    @Override
    public String testedMehtod(){
        System.out.println("Enter UserServiceImpl testedMehtod ...");
        ProcessDB processDB = new ProcessDB("BB");
        processDB.getResultOfConnectDBNoReturn("AA");
        return processDB.getResultOfConnectDB();
    }

}

BussinessService.java

package com.github.mock.simple.user.impl;

import com.github.mock.simple.vo.ProcessDB;

public class BussinessService {
    public String testedMehtod() {
        System.out.println("Enter BussinessService testedMehtod ...");
        ProcessDB processDB = new ProcessDB("BB");
        processDB.getResultOfConnectDBNoReturn("AA");
        return processDB.getResultOfConnectDB();
    }
}
  • 测试类:

MockSpringSimpleTest.java

package com.github.mock.simple.test;

import java.text.MessageFormat;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.github.mock.simple.user.IUserService;
import com.github.mock.simple.user.impl.BussinessService;
import com.github.mock.simple.user.impl.UserServiceImpl;
import com.github.mock.simple.vo.ProcessDB;

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)//Spring上下文
@PrepareForTest({BussinessService.class,UserServiceImpl.class})
@ContextConfiguration(locations = {"classpath:applicationContext-mock-inject.xml"})
public class MockSpringSimpleTest {

    //使用Spring上下文
    @Autowired
    IUserService userService;

    @Mock
    ProcessDB processDB;
    
    //不使用Spring上下文时,使用该注解
    @InjectMocks
    private BussinessService bussinessService;

    @Before
    public void initMocks() throws Exception {
        MockitoAnnotations.initMocks(this);
        //ReflectionTestUtils.setField(userService, "processDB", processDB);
        PowerMockito.whenNew(ProcessDB.class).withArguments("BB").thenReturn(processDB);
        // PowerMockito.whenNew(ProcessDB.class).withNoArguments().thenReturn(processDB);
    }

    @Test
    public void mockConnectDB() {
        String aa = "haha, everything is fake"; 
        PowerMockito.when(processDB.getResultOfConnectDB()).thenReturn(aa);
        PowerMockito.doNothing().when(processDB).getResultOfConnectDBNoReturn("AA");
        System.out.println(bussinessService.testedMehtod());
        Assert.assertEquals("haha, everything is fake", bussinessService.testedMehtod());
    }

    @Test
    public void mockConnectDB2() {
        try {
            String aa = "haha, everything is fake";
            PowerMockito.when(processDB.getResultOfConnectDB()).thenReturn(aa);
            PowerMockito.doNothing().when(processDB).getResultOfConnectDBNoReturn("AA");
            System.out.println(userService.testedMehtod());
            Assert.assertEquals("haha, everything is fake", userService.testedMehtod());
        } catch (Exception ex) {
            System.out.println("--- getMessage ---");
            System.out.println(ex.getMessage());
            System.out.println();
            
            System.out.println("--- toString ---");
            System.out.println(ex.toString());
            System.out.println();
            
//            System.out.println("--- printStackTrace ---");
//            StringWriter stringWriter = new StringWriter();
//            PrintWriter printWriter = new PrintWriter(stringWriter);
//            ex.printStackTrace(printWriter);
//            System.out.println(stringWriter.toString());
//            System.out.println();
            
            System.out.println("--- printStackTrace DIY ---");
            System.out.println(ex.getClass().getName() + ": " + ex.getMessage());
            StringBuilder sbException = new StringBuilder();
            for (StackTraceElement ele : ex.getStackTrace()) {
                sbException.append(MessageFormat.format("tat {0}.{1}({2}:{3})n", 
                    ele.getClassName(), ele.getMethodName(), ele.getFileName(), ele.getLineNumber()));;
            }
            System.out.println(sbException);
            
            sbException = null;
//            stringWriter = null;
//            printWriter = null;
        }
    }

}
  • 扫描注入xml

最后applicationContext-mock-inject.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
	http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd  
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.github.mock.simple"/>

</beans>

对于没有实现类,但又被依赖的接口,在applicationContext-mock-inject.xml添加如下内容 (本文不需要):

<bean name="iXxService" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="com.github.mock.simple.api.IXxService"/>
</bean>

同时在测试类里面添加下面的代码:

@Mock
iXxService iXxService;

在 @Before里面添加下面的代码

ReflectionTestUtils.setField(userService, "iXxService", iXxService);
  • 测试结果

最后

以上就是干净小白菜为你收集整理的PowerMockito的基本使用的全部内容,希望文章能够帮你解决PowerMockito的基本使用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部