PowerMock实践

PowerMock

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <powermock.version>1.6.3</powermock.version>
    </properties>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>${powermock.version}</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-easymock</artifactId>
            <version>${powermock.version}</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>${powermock.version}</version>
        </dependency>
        <!--<dependency>-->
            <!--<groupId>org.easymock</groupId>-->
            <!--<artifactId>easymock</artifactId>-->
            <!--<version>2.5.2</version>-->
        <!--</dependency>-->
        <!--<dependency>-->
        <!--<groupId>org.mockito</groupId>-->
        <!--<artifactId>mockito-all</artifactId>-->
        <!--<version>1.10.19</version>-->
        <!--</dependency>-->

package com.zte.sunquan.demo.mock;

import java.io.File;

/**
 * Created by sunquan on 2017/12/6.
 */
public class UserUtils {
    private String name;

    public UserUtils() {
    }

    public UserUtils(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public static String getOutput() {
        return "hello,world";
    }

    public boolean isExit(File file) {
        return file.exists();
    }

    public String copyFile(File file) {
        if (isExit(file))
            return "success";
        else
            return "failure";
    }
}

package com.zte.sunquan.demo.mock;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;

import java.io.File;

/**
 * Created by sunquan on 2017/12/6.
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({UserUtils.class})
public class MockTest {
    @Test
    public void testMock1() {
        //静态方法
        PowerMockito.mockStatic(UserUtils.class);
        PowerMockito.when(UserUtils.getOutput()).thenReturn("boy");
        System.out.println(UserUtils.getOutput());
        UserUtils mock = PowerMockito.mock(UserUtils.class);
        PowerMockito.when(mock.getName()).thenReturn("girl");
        System.out.println(mock.getName());
    }

    @Test
    public void testMock2() {
        //普通方法
        UserUtils mock = PowerMockito.mock(UserUtils.class);
        PowerMockito.when(mock.getName()).thenReturn("girl");
        System.out.println(mock.getName());
    }

    @Test
    public void testMock3() {
        //方法参数1
        UserUtils utils = new UserUtils();
        File mock = PowerMockito.mock(File.class);
        PowerMockito.when(mock.exists()).thenReturn(false);
        System.out.println(utils.isExit(mock));
    }

    @Test
    public void testMock4() {
        //方法参数2
        UserUtils utils = new UserUtils();
        File mock = PowerMockito.mock(File.class);
        PowerMockito.when(mock.exists()).thenReturn(true);
        System.out.println(utils.copyFile(mock));
    }

    @Test
    public void testMock5() throws Exception {
        //有参数的方法1
        UserUtils mock = PowerMockito.mock(UserUtils.class);
        PowerMockito.when(mock, "isExit", Matchers.any(File.class)).thenReturn(true);
        PowerMockito.when(mock.copyFile(Matchers.any(File.class))).thenCallRealMethod();
        System.out.println(mock.copyFile(Matchers.any(File.class)));
    }

    @Test
    public void testMock6() throws Exception {
        //有参数的方法2
        UserUtils mock = PowerMockito.mock(UserUtils.class);
        PowerMockito.whenNew(UserUtils.class).withArguments("sunquan").thenReturn(mock);
        PowerMockito.when(mock.getName()).thenReturn("wang");
        System.out.println(new UserUtils("sunquan").getName());
    }

    @Test
    public void testMock7() throws Exception {
        //设置参数
        UserUtils utils = new UserUtils();
        Whitebox.setInternalState(utils, "name", "sunquan");
        System.out.println(utils.getName());
    }


    @Test
    public void testMock8() throws Exception {
        //定制返回值
        int i = 0;
        UserUtils mock = PowerMockito.mock(UserUtils.class);
        PowerMockito.whenNew(UserUtils.class).withArguments("sunquan").thenReturn(mock);
        PowerMockito.when(mock.getName()).thenAnswer(p -> {
//            String name = (String) p.getArguments()[0];
//            System.out.println(name);
//            if (name.equals("sunquan"))
//                return "sunquan1";
//            else
//                return null;
            if (i == 0)
                return "sunquan+0";
            String result = (String) p.callRealMethod();
            return result;

        });
        System.out.println(new UserUtils("sunquan").getName());
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值