Mockito 是一個流行的 Java 模擬測試框架,用于創建和管理測試中的模擬對象(mock objects)。它可以幫助開發者編寫干凈、可維護的單元測試,特別是在需要隔離被測組件與其他依賴項時。
目錄
核心概念
1. 模擬對象(Mock Objects)
2. 打樁(Stubbing)
3. 驗證(Verification)
1. 測試準備
2. 模擬依賴(Mock)
3. 執行測試?
4. 驗證結果?
5. 驗證收藏數增加?
總結
核心概念
1. 模擬對象(Mock Objects)
模擬對象是真實對象的替代品,用于在測試中模擬真實對象的行為,而不需要實際調用真實對象。
2. 打樁(Stubbing)
定義模擬對象在特定條件下的行為,即當調用某個方法時返回什么值或拋出什么異常。
3. 驗證(Verification)
驗證模擬對象的特定方法是否被調用,以及調用的次數和參數是否符合預期。
下面進行實際操作
等待測試的源碼:
@Overridepublic JSONReturn save(Collect collect) {Collect exist = collectMapper.selectExist(collect.getUserId(),collect.getForumId());if(exist != null){return JSONReturn.failed("操作失敗,您已收藏該帖子!");}Date date = new Date();collect.setCreateTime(date);collect.setUpdateTime(date);collectMapper.insert(collect);
// //收藏+1Forum forum = forumMapper.selectByPrimaryKey(collect.getForumId());forum.setCollectNum(forum.getCollectNum() + 1);forumMapper.updateByPrimaryKeySelective(forum);return JSONReturn.success("收藏成功!");}
?以下下是mock的代碼
@Testvoid save_shouldReturnSuccess_whenCollectNotExists() {Collect collect = new Collect();collect.setUserId(2);collect.setForumId(9);when(collectMapper.selectExist(2, 9)).thenReturn(null); Forum forum = new Forum();forum.setCollectNum(0);when(forumMapper.selectByPrimaryKey(9)).thenReturn(forum);JSONReturn result = collectService.save(collect);assertEquals("收藏成功!", result.getMsg()); // Should return success messageverify(collectMapper, times(1)).selectExist(2, 9);verify(collectMapper, times(1)).insert(collect);verify(forumMapper, times(1)).updateByPrimaryKeySelective(any(Forum.class));ArgumentCaptor<Forum> forumCaptor = ArgumentCaptor.forClass(Forum.class);verify(forumMapper).updateByPrimaryKeySelective(forumCaptor.capture());Integer collectNum = forumCaptor.getValue().getCollectNum();assertEquals(new Integer(1), collectNum);}
這是一個單元測試方法,測試當收藏記錄不存在時成功保存收藏的功能。下面是對代碼的逐步解釋:
1. 測試準備
Collect collect = new Collect();
collect.setUserId(2);
collect.setForumId(9);
創建一個新的收藏對象,設置用戶ID為2,論壇ID為9。
2. 模擬依賴(Mock)
when(collectMapper.selectExist(2, 9)).thenReturn(null);
模擬collectMapper.selectExist(2, 9)
方法調用,返回null表示該收藏記錄不存在。
Forum forum = new Forum();
forum.setCollectNum(0);
when(forumMapper.selectByPrimaryKey(9)).thenReturn(forum);
創建一個論壇對象,設置收藏數為0,并模擬forumMapper.selectByPrimaryKey(9)
返回這個論壇對象。
3. 執行測試?
JSONReturn result = collectService.save(collect);
調用被測試的collectService.save()
方法。
4. 驗證結果?
assertEquals("收藏成功!", result.getMsg());
驗證返回的消息是"收藏成功!"。
verify(collectMapper, times(1)).selectExist(2, 9);
verify(collectMapper, times(1)).insert(collect);
驗證:
-
selectExist
方法被調用了一次 -
insert
方法被調用了一次
verify(forumMapper, times(1)).updateByPrimaryKeySelective(any(Forum.class));
驗證updateByPrimaryKeySelective
方法被調用了一次。
5. 驗證收藏數增加?
ArgumentCaptor<Forum> forumCaptor = ArgumentCaptor.forClass(Forum.class);
verify(forumMapper).updateByPrimaryKeySelective(forumCaptor.capture());
Integer collectNum = forumCaptor.getValue().getCollectNum();
assertEquals(new Integer(1), collectNum);
使用ArgumentCaptor
捕獲傳遞給updateByPrimaryKeySelective
的Forum對象,驗證其收藏數從0增加到了1。
總結
這個測試驗證了當用戶收藏一個尚未收藏的論壇時:
-
系統會檢查該收藏記錄是否存在
-
如果不存在則創建新收藏記錄
-
更新論壇的收藏數(+1)
-
返回成功消息
測試覆蓋了收藏服務的主要邏輯路徑,確保在收藏記錄不存在時的正確行為。
?
?
?
?
?