????????????????????--Mockito??????
?????????????? ???????[ 2017/6/8 13:39:35 ] ??????????????? ???????
?????????????Mockito?
???????д??????????????????????????????????????????Щ????????????????????????????????????????????????????????????????????.??????????????????????????????????????????(???RPC????)??????д?????????????????????y??????????rpc?????mock??????????????????????????д????y???????????????????????????????????????Mockito????????????????????????mock???????mockito???????????????????.
????????mock????
?????????????????????Redis????????????????????????:
publicclassRedisUtil{
????@Autowired
????privateRedisOperations<String??String>redisTemplate;
????publicbooleancheckKeyExists(Stringkey){
????returnredisTemplate.hasKey(key);
????}
????publicvoidsetValueByKey(Stringkey??Stringvalue){
????redisTemplate.opsForValue().set(key??value);
????}
????publicStringgetValueByKey(Stringkey){
????returnredisTemplate.opsForValue().get(key);
????}
????publicList<String>getMutiValuesByList(List<String>keys){
????returnredisTemplate.opsForValue().multiGet(keys);
????}
????publicvoiddeleteKey(Stringkey){
????redisTemplate.delete(key);
????}
????publicvoidincreValue(Stringkey??Longcount){
????redisTemplate.opsForValue().increment(key??count);
????}
????}
???????????????????????mock?????????????
????Mockito.mock(RedisUtil.class);
??????????????????????@mock???:
????@mock
????privateRedisUtilredisUtil;
????Tips:??????????????????Mockito????????????????package????????????????????????Mockito??:
????importstaticorg.mockito.Mockito.*;
????????????????????÷???
??????????????????????????г??????????????????????mock?????????Щ?????????????????????????????е??????????????б????????????????.
??????????RedisUtil???????????趨?????:
when(redisUtil.getValueByKey("key1")).thenReturn("value1");
????when(redisUtil.getValueByKey("key2")).thenReturn("value2");
??????????趨???????????????:
????when(redisUtil.getValueByKey("key1")).thenThrow(newRuntimeException);
????????Mockito????????????????????:
????when(redisUtil.getValueByKey("key1")).thenReturn("value1").thenReturn("Value2");
????????????????ε???redis.getValueBykey("key1")?????value1??????α???????????value2.?????????????????????????????????????????value2???????????????????????return??Throw??????????????????????????????????????????????????????λ?????????????.
????Mockito???mock??????void?????
??????????????ж????????void?????????mock???????????Ч?????Mockito?????doNothing??????void????????????:
??doNothing().when(redisUtil).increValue(“key1"??1L);
??????????doNothing??mock?????????????????壬???????mock??????????????????????????????????????????????????????????????????????????????????????????????????????μ?????????????????????void???????????????????mock?????????doThrow()???void?????????????????????????mock????????????????????????????????????ú?verify()??£??????????????????????.????
????verify(redisUtil??times(1)).increValue("key1"??1L);
?????????е?times(1)?????Σ?????????increValue()???????????ε?????????????????????????????壬
????never():?????δ??????
????atleastOnce():???????????????
????atleast(3):????????????3??
????atMost(7):?????????7??
?????????????
??????????????????????????????????????????????????????????redisUtil?????????redisUtil.getValueByKey???????????????????key?????????????????????д:
????when(redisUtil.getValueByKey(anyString()).thenReturn("value1")
????????????????????У????????????????????÷?????????value1?????????????к??anyLong()??anyInt()??anyList()???
???????Spy????????????????
???????????????????mock????mock?????????????????????????????????????Spy?????????????????????????????????????????????????????????????????????????????????????????????spy?????????mock???????????????spy???????????????????????CLass????.?????????List?????
?? Listspy=spy(newLinkedList());
????when(spy.get(0)).thenReturn("value1");
????doReturn("value2").when(spy).get(0);
???????????д???????when(spy.get(0))?????????????????????????????????д???????????????????????????value2.??????????????????when????????????????????????????????doReturn()????????????????????????.
??????????????????t??????????spy.
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11