PowerMock笔记-ArgumentsMatcher

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/Andy2019/article/details/80316004


import lombok.Data;

/**
 * @author chenjianfei
 */
@Data
public class Employee {
    private String name;
    private Integer age;
}
public class EmployeeDao {
    public String findByName(String name) {
        throw new UnsupportedOperationException();
    }
}
public class EmployeeService {

    public String find(String name) {
        EmployeeDao employeeDao = new EmployeeDao();
        return employeeDao.findByName(name);
    }
}

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.ArgumentMatchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.*;

@RunWith(PowerMockRunner.class)
@PrepareForTest(EmployeeService.class)
public class EmployeeServiceTest {

    @Test
    public void findByName() {
        try {
            EmployeeDao employeeDao = PowerMockito.mock(EmployeeDao.class);
            PowerMockito.whenNew(EmployeeDao.class).withAnyArguments().thenReturn(employeeDao);

            EmployeeService employeeService = new EmployeeService();

            PowerMockito.when(employeeDao.findByName("andy")).thenReturn("haha");
            String result = employeeService.find("andy");
            Assert.assertEquals("haha", result);

            PowerMockito.when(employeeDao.findByName("Tom")).thenReturn("haha");
            String result2 = employeeService.find("Tom");
            Assert.assertEquals("haha", result2);
        } catch (Exception e) {
            Assert.fail();
        }
    }

    /**
     * 使用Matcher
     */
    @Test
    public void findByNameWithMatcher() {
        try {
            EmployeeDao employeeDao = PowerMockito.mock(EmployeeDao.class);
            PowerMockito.whenNew(EmployeeDao.class).withAnyArguments().thenReturn(employeeDao);
            PowerMockito.when(employeeDao.findByName(ArgumentMatchers.argThat(new MyArgumentMatcher()))).thenReturn("hehe");
            EmployeeService employeeService = new EmployeeService();
            Assert.assertEquals("hehe",employeeService.find("Tom"));
            Assert.assertEquals("hehe",employeeService.find("Andy"));
            Assert.assertEquals("hehe",employeeService.find("Tony"));
//            Assert.assertEquals("hehe",employeeService.find("Boss"));
        } catch (Exception e) {
            Assert.fail();
        }
    }

    static class MyArgumentMatcher implements ArgumentMatcher<String> {
        public boolean matches(String argument) {
            switch (argument) {
                case "Tom":
                case "Andy":
                case "Tony":
                    return true;
                default:
                    return false;
            }
        }
    }
}



阅读更多

扫码向博主提问

Andy2019

非学,无以致疑;非问,无以广识
  • 擅长领域:
  • Java后端开发
去开通我的Chat快问

没有更多推荐了,返回首页