Bootstrap

mock单元测试

1. 导入依赖

<!-- Mockito 2 -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>2.19.0</version>
    <scope>test</scope>
</dependency>
<!-- PowerMock TestNG Module -->
<dependency>
    <groupId>org.powermock</groupId> 
    <artifactId>powermock-module-testng</artifactId>
    <version>2.0.0-beta.5</version>
    <scope>test</scope></dependency>
<!-- PowerMock JUnit 4.4+ Module -->
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>2.0.0-beta.5</version>
    <scope>test</scope>
</dependency>
<!-- PowerMock Mockito2 API -->
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito2</artifactId>
    <version>2.0.0-beta.5</version>
    <scope>test</scope>
</dependency>

2. 环境准备

实体类 TestUtil

package com.test.util;

public class TestUtil {

    public static String toLowercase(String param) {
        if(param==null){
            throw new IllegalArgumentException(); 
        }
        return param.toLowerCase();
    }
    
    public static String sayBye(String param) {
        return "...";
    }
}

实体类 TestController

package com.test.controller;

import com.test.service.TestService;
import com.test.util.TestUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Value;

/**
 * @author [email protected]
 */
@RestController
@RequestMapping("test")
public class TestController {
    
    @Value("${user}")
    private String user;
    
    @GetMapping("greet")
    public String method1(String param) {
        return user+":"+TestUtil.toLowercase(param);
    }
    
    @GetMapping("bye")
    public String method2(String param) {
        return TestUtil.sayBye(param);
    }
}

3. 接口测试

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.test.util.ReflectionTestUtils;

import static org.mockito.Mockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest(TestUtil.class)
public class TestControllerTest {
private static Logger log = LoggerFactory.getLogger(TestControllerTest.class);
@InjectMocks    
private TestController testController;
/*
	Mock对象 在调用testService是的方法打桩使用
*/
@Mocks
private TestService testService;
@Before    
public void setUp() {
    /*
    	需要调用某些Class静态方法
    		1.需要在@PrepareForTest中添加该Class
    		2.使用PowerMockiop.mockStatic加载该Class
    */
	PowerMockito.mockStatic(TestUtil.class);
}
/**
* 测试有返回值方法的接口调用测试
*/
@Test
public void method1(){
	/*
		Mock TestController中@Value的属性值:
			使用ReflectionTestUtils.setField(Object targetObject, String name,Object value)
	*/
    String user="yue"
    ReflectionTestUtils.setField(TestController, "user", user);
    String param="hello world";
    /*
    	打桩:设计工具类传入参数和返回值
    */
  	when(TestUtil.toLowercase(param)).thenReturn(param);
    /*
    	打桩:设计工具类抛出异常
    */
    when(TestUtil.Lowercase (null)).thenThrow(new IllegalArgumentException());
    /*
    	调用测试
    */
    String result=testController.method1(param);
    /*
    	使用Assert确认测试结果
    */
    Assert.assertEquals(result,user":"+param);
}
/**
* 测试无返回值方法的接口调用测试
*/
@Test
public void method2(){
	String param="hello world";
    Mockito.doNothing().when(TestUtil).(sayBye);
    String result=testController.method2(param);
    Assert.assertEquals(result,...);
}
}   
;