做单元测试时候,经常会mock一些方法,得到期望的返回值,这里记录一下常见的public、private、static方法,以及设计到的内部调用的方法
一、依赖坐标引入
这里采用maven,引入powermock即可
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
二、BaseTest基类编写
所有单元测试的测试类都继成BaseTest即可
(1)springboot的示例
import com.abc.WebApplication;
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.boot.test.context.SpringBootTest;
@RunWith(PowerMockRunner.class)
@SpringBootTest(classes = WebApplication.class)
public abstract class BaseTest {
}
(2)springmvc的示例
import org.junit.runner.RunWith;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.test.context.ContextConfiguration;
@RunWith(PowerMockRunner.class)
@ContextConfiguration({"classpath:spring.xml","classpath:spring-mvc.xml"})
public class BaseTest {
}
三、单元测试编写
有如下类需要编写单元测试、有三个方法,分别是public、private、static,
mock的类涉及到private和static的,需要在测试类上增加注解@PrepareForTest({私有和静态方法所在的类.class})
(1)需要被单测的类
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service
public class MockTestServiceImpl implements MockTestService {
@Resource
private RpcTestGoodPrice rpcTestGoodPrice;
@Resource
private TestUtil testUtil;
public String queryRpcPrice(String skuId) {
String queryRpcPrice = rpcTestGoodPrice.queryRpcPrice(skuId);
return queryRpcPrice;
}
public void printRpcPrice(String skuId) {
String queryRpcPrice = rpcTestGoodPrice.queryRpcPrice(skuId);
System.out.println(queryRpcPrice);
}
private String queryRpcSkuPrice(String skuId) {
String queryRpcPrice = rpcTestGoodPrice.queryRpcPrice(skuId);
return queryRpcPrice;
}
public String querySkuName(String skuId) {
String skuName = MockTestServiceImpl.getSkuName(skuId);
return skuName;
}
public static String getSkuName(String skuId) {
return "";
}
public void printRpcPrice(String skuId) {
testUtil.printPrice(skuId);
}
}
内部涉及到需要调用的其它类
import org.springframework.stereotype.Service;
@Service
public class RpcTestGoodPrice {
@Resource
private GoodPriceProvider goodPriceProvider;
public String queryRpcPrice(String skuId) {
PriceResult priceResult = goodPriceProvider.queryPrice(skuId);
return priceResult.getPrice();
}
}
(2)mock公有方法
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.springframework.test.util.ReflectionTestUtils;
public class MockTestServiceImplTest extends BaseTest {
@InjectMocks
private MockTestServiceImpl mockTestServiceImpl;
@InjectMocks
private RpcTestGoodPrice rpcTestGoodPrice;
@Mock
private GoodPriceProvider goodPriceProvider;
@Test
public void testPublic() {
//预先设置成员变量,可以写在@Before中
ReflectionTestUtils.setField(mockTestServiceImpl, "rpcTestGoodPrice", rpcTestGoodPrice);
Mockito.when(goodPriceProvider.queryPrice(Mockito.any())).thenAnswer((m) -> {
PriceResult result = new PriceResult();
result.setPrice("22.99");
return result;
});
String skuId = "12345679";
String queryRpcPrice = mockTestServiceImpl.queryRpcPrice(skuId);
Assert.assertEquals("22.99", queryRpcPrice);
}
}
(3)mock私有方法
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.springframework.test.util.ReflectionTestUtils;
@PrepareForTest({MockTestServiceImpl.class})
public class MockTestServiceImplTest extends BaseTest {
@InjectMocks
private MockTestServiceImpl mockTestServiceImpl;
@InjectMocks
private RpcTestGoodPrice rpcTestGoodPrice;
@Mock
private GoodPriceProvider goodPriceProvider;
@Test
public void testPrivate() {
MockTestServiceImpl mockTestServiceImpl = PowerMockito.spy(new MockTestServiceImpl());
//预先设置成员变量,可以写在@Before中
ReflectionTestUtils.setField(mockTestServiceImpl, "rpcTestGoodPrice", rpcTestGoodPrice);
Mockito.when(goodPriceProvider.queryPrice(Mockito.any())).thenAnswer((m) -> {
PriceResult result = new PriceResult();
result.setPrice("22.99");
return result;
});
String skuId = "12345679";
try {
PowerMockito.when(PowerMockito.spy(new MockTestServiceImpl()),"queryRpcSkuPrice", skuId).thenAnswer((m) -> {
return "23.99";
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
(4)mock静态方法
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
@PrepareForTest({MockTestServiceImpl.class})
public class MockTestServiceImplTest extends BaseTest {
@InjectMocks
private MockTestServiceImpl mockTestServiceImpl;
@Test
public void testStatic() {
PowerMockito.mockStatic(MockTestServiceImpl.class);
PowerMockito.when(MockTestServiceImpl.getSkuName(Mockito.anyString())).thenAnswer((m) -> {
return "商品名称";
});
String skuId = "12345679";
String querySkuName = mockTestServiceImpl.querySkuName(skuId);
Assert.assertEquals("商品名称", querySkuName);
}
}
(5)mock无返回值的方法
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
public class MockTestServiceImplTest extends BaseTest {
@InjectMocks
private MockTestServiceImpl mockTestServiceImpl;
@Mock
private TestUtil testUtil;
@Test
public void testVoidPublic() {
//预先设置成员变量,可以写在@Before中
Mockito.doNothing().when(testUtil).printPrice(Mockito.any());
mockTestServiceImpl.printRpcPrice("12345678");
}
}
(6)mock线程池内的方法文章来源:https://www.toymoban.com/news/detail-433520.html
public CmsResponse downloadData() {
threadPool.execute(()-> {
});
return new Response(ResponseEnum.SUCCESS.getCode(),"下载中,请稍后");
}
在单测中加入以下代码,延缓主线程5-10秒,不然主线程结束后,子线程就结束了,单测执行不了文章来源地址https://www.toymoban.com/news/detail-433520.html
try {
Thread.currentThread().join(8000);
} catch (Exception e) {
e.printStackTrace();
}
@Test
public CmsResponse downloadDataTest() {
downloadDataServiceImpl.downloadData();
try {
Thread.currentThread().join(8000);
} catch (Exception e) {
e.printStackTrace();
}
}
到了这里,关于junit单元测试mock常用写法-springboot与springmvc的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!