mock: 对接口返回值进行模拟数据。
使用场景:
1.协同开发的时候,需要调用未开发接口,模拟接口返回值数据。
2.接口联调,网络不通的时候,把线上数据拷贝回来,模拟数据。
添加mockito依赖:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.3.3</version>
<scope>compile</scope>
</dependency>
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.springframework.web.bind.annotation.RequestBody;
import springfox.documentation.annotations.ApiIgnore;
import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
/**
* @Description
* @Author keepon
* @Date 2021/10/22 14:08
*/
public class CorpCreditRecoverControllerTest {
@Mock
private CreditRecoverService creditRecoverService;
@Before
public void setUp(){
creditRecoverService=mock(CreditRecoverService.class);
CreditSearchResultDTO<CorpRecoverDTO> creditSearchResultDTO = new CreditSearchResultDTO();
creditSearchResultDTO.setSumTotal(BigDecimal.valueOf(123));
RestResult<CreditSearchResultDTO<CorpRecoverDTO>> creditSearchResultDTORestResult = new RestResult<>();
creditSearchResultDTORestResult.setData(creditSearchResultDTO);
when(creditRecoverService.search(any())).thenReturn(creditSearchResultDTORestResult);
//when(creditRecoverService.search(any()).tryResult()).thenReturn(creditSearchResultDTO); 不允许包括tryResult(),会报错。
//或者在创建mock对象的时候,增加一个构造参数RETURNS_DEEP_STUBS,RETURNS_DEEP_STUBS,RETURNS_DEEP_STUBS参数程序会自动进行mock所需的对象
//例如这样creditRecoverService=mock(CreditRecoverService.class,RETURNS_DEEP_STUBS);
}
/**
* 验证期待返回值,结合接口
*/
@Test
public void appliedSearch(){
SearchParameter<CorpRecoverRequestVO> requestVO = new SearchParameter<>();
CorpRecoverQueryDTO corpRecoverQueryDTO = new CorpRecoverQueryDTO();
CreditSearchResultDTO<CorpRecoverDTO> result= creditRecoverService.search(new SearchParameter<>(corpRecoverQueryDTO, requestVO.getPageCond())).tryResult();
List<CorpRecoverResponseVO> list = BeanUtils.mapc(result.getDatalist(), CorpRecoverResponseVO.class);
PageCond pageCond = result.getPagecond();
CreditSearchResult<CorpRecoverResponseVO> response = new CreditSearchResult<>(list, pageCond);
response.setSumTotal(result.getSumTotal());
Assert.assertEquals(response.getSumTotal(),BigDecimal.valueOf(123));
response.setSumTotal(result.getSumTotal());
}
/**
* 验证行为
*/
@Test
public void verify_behaviour(){
//模拟创建一个List对象
List mock = mock(List.class);
//使用mock的对象
mock.add(1);
mock.clear();
//验证add(1)和clear()行为是否发生
verify(mock).add(1);
verify(mock).add(1);
// verify(mock).add(2); 就会报错,因为确实没有执行过add(2)的操作
verify(mock).clear();
}
/**
* 验证期待返回值
*/
@Test
public void when_thenReturn(){
//mock一个Iterator类
Iterator iterator = mock(Iterator.class);
//预设当iterator调用next()时第一次返回hello,第n次都返回world
when(iterator.next()).thenReturn("hello").thenReturn("world");
//使用mock的对象
String result = iterator.next() + " " + iterator.next() + " " + iterator.next();
//验证结果
assertEquals("hello world world",result);
}
/**
* 逻辑值判断,当入参符合特定条件时,返回特定结果值
*
*/
@Test
public void unstubbed_invocations(){
//mock对象使用Answer来对未预设的调用返回默认期望值
List mock = mock(List.class,new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return 999;
}
});
//下面的get(1)没有预设,通常情况下会返回NULL,但是使用了Answer改变了默认期望值
assertEquals(999, mock.get(1)); //符合
//下面的size()没有预设,通常情况下会返回0,但是使用了Answer改变了默认期望值
assertEquals(999,mock.size()); //符合
List mock1=mock(List.class);
when(mock1.get(mock.size())).thenReturn("dfsfd");
Assert.assertEquals("dfsfd",mock1.get(999)); //符合特定条件,返回true
Assert.assertEquals("dfsfd1",mock1.get(99)); //不符合特定条件,false,抛出异常
}
}