日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
在Spring中進(jìn)行集成測試

在單元測試時,我們盡量在屏蔽模塊間相互干擾的情況下,重點關(guān)注模塊內(nèi)部邏輯的正確性。而集成測試則是在將模塊整合在一起后進(jìn)行的測試,它的目的在于發(fā)現(xiàn)一些模塊間整合的問題。有些功能很難通過模擬對象進(jìn)行模擬,相反它們往往只能在真實模塊整合后,才能真正運(yùn)行起來,如事務(wù)管理就是其中比較典型的例子。

“專業(yè)、務(wù)實、高效、創(chuàng)新、把客戶的事當(dāng)成自己的事”是我們每一個人一直以來堅持追求的企業(yè)文化。 成都創(chuàng)新互聯(lián)是您可以信賴的網(wǎng)站建設(shè)服務(wù)商、專業(yè)的互聯(lián)網(wǎng)服務(wù)提供商! 專注于成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、軟件開發(fā)、設(shè)計服務(wù)業(yè)務(wù)。我們始終堅持以客戶需求為導(dǎo)向,結(jié)合用戶體驗與視覺傳達(dá),提供有針對性的項目解決方案,提供專業(yè)性的建議,創(chuàng)新互聯(lián)建站將不斷地超越自我,追逐市場,引領(lǐng)市場!

按照Spring的推薦(原話:You should not normally use the Spring container for unit tests: simply populate your POJOs in plain JUnit tests!),在單元測試時,你不應(yīng)該依賴于Spring容器。換言之,你不應(yīng)該在單元測試時啟動ApplicatonContext并從中獲取 Bean,相反你應(yīng)該通過模擬對象完成單元測試。而集成測試的前提則是事先裝配好模塊和模塊之間的關(guān)聯(lián)類,如將DAO層真實的UserDao和 LoginLogDao裝配到UserServiceImpl再進(jìn)行測試。具體裝配工作是在Spring配置文件中完成的,因此在一般情況下,集成測試需要啟動Spring容器,你可以在測試類中簡單地從Spring容器中取出目標(biāo)Bean進(jìn)行測試。

需要測試的業(yè)務(wù)接口

假設(shè)我們的應(yīng)用中擁有一個UserService業(yè)務(wù)層接口,它擁有4個業(yè)務(wù)方法,其代碼如下所示:
代碼清單1 UserServie接口

  1. package com.baobaotao.service;  
  2. import com.baobaotao.domain.User;  
  3. import org.springframework.transaction.annotation.Transactional;  
  4. @Transactional  
  5. public interface UserService {  
  6. boolean hasMatchUser(String userName,String password);  
  7. User findUserByUserName(String userName);  
  8. void loginSuccess(User user);  
  9. void registerUser(User user);  

我們通過UserServiceImpl對UserService提供了實現(xiàn):
代碼清單2 UserServiceImpl實現(xiàn)UserService接口 

  1. package com.baobaotao.service;  
  2. import com.baobaotao.dao.LoginLogDao;  
  3. import com.baobaotao.dao.UserDao;  
  4. import com.baobaotao.domain.LoginLog;  
  5. import com.baobaotao.domain.User;  
  6. public class UserServiceImpl implements UserService {  
  7. private UserDao userDao;  
  8. private LoginLogDao loginLogDao;  
  9. public boolean hasMatchUser(String userName, String password) {  
  10. int matchCount =userDao.getMatchCount(userName, password);  
  11. return matchCount > 0;  
  12. }  
  13. public User findUserByUserName(String userName) {  
  14. return userDao.findUserByUserName(userName);  
  15. }  
  16. public void loginSuccess(User user) {  
  17. user.setCredits( 5 + user.getCredits());  
  18. LoginLog loginLog = new LoginLog();  
  19. loginLog.setUserId(user.getUserId());  
  20. loginLog.setIp(user.getLastIp());  
  21. loginLog.setLoginDate(user.getLastVisit());  
  22. userDao.updateLoginInfo(user);  
  23. loginLogDao.insertLoginLog(loginLog);  
  24. }  
  25. public void setLoginLogDao(LoginLogDao loginLogDao) {  
  26. this.loginLogDao = loginLogDao;  
  27. }  
  28. public void setUserDao(UserDao userDao) {  
  29. this.userDao = userDao;  
  30. }  
  31. }  

UserServiceImpl引用了兩個DAO層的類(UserDao和LoginLogDao)共同實現(xiàn)UserService的接口,在UserServiceImpl開放使用之前,我們有必須對其進(jìn)行集成測試,以保證實現(xiàn)邏輯的正確性。


網(wǎng)頁標(biāo)題:在Spring中進(jìn)行集成測試
標(biāo)題來源:http://www.dlmjj.cn/article/dheggsg.html