PowerMock实践

来源:互联网 发布:网络推广的面试问题 编辑:程序博客网 时间:2024/04/25 15:50

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());    }}