mockito mock void方法_纯干货,浅谈Mockito在单元测试中的实际应用
生活随笔
收集整理的這篇文章主要介紹了
mockito mock void方法_纯干货,浅谈Mockito在单元测试中的实际应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文接上文"接口方沒寫代碼,對接方只能停工嗎?",在這里簡單介紹Mockito在單元測試中的實際應用。本文使用場景較單一,如有雷同,不甚榮幸,閑言少敘,開門見山。本文將使用mock技術模擬三種場景,1:模擬數據庫操作對象返回。2:模擬業務層操作。3:調用及實例分析。第一步:mock環境依賴,僅導入mockito最小依賴包。配置如下:<dependencies> <dependency> <groupId>org.mockitogroupId> <artifactId>mockito-allartifactId> <version>1.9.5version> dependency> <dependency> <artifactId>junitartifactId> <groupId>junitgroupId> <version>4.11version> dependency>dependencies>第二步,相應數據庫操作類和業務層邏輯代碼。StudentDao.javapackage com.yelang.mocktest.dao;import com.yelang.mocktest.entity.Student;/** * 模擬數據庫訪問層 * @author asus */public class StudentDao { /** * 根據id查詢學生信息 * @param pkId * @return * @throws Exception */ public Student findByID(Long pkId) throws Exception{ Student result = null; if(pkId == null) { return result; } result = new Student(pkId, "楊" + pkId, Short.valueOf("1"), "湖南省長沙市岳麓區", 18); return result; }}ClassInfoService.javapackage com.yelang.mocktest.service;import com.yelang.mocktest.dao.StudentDao;import com.yelang.mocktest.dao.TeacherDao;import com.yelang.mocktest.entity.Student;import com.yelang.mocktest.entity.Teacher;public?class?ClassInfoService?{??????private?StudentDao?studentDao; private TeacherDao teacherDao; public void queryInfo() throws Exception { Long tempPkId = 6L; Student student = studentDao.findByID(tempPkId); System.out.println(student.getName()); Teacher teacher = teacherDao.findById(tempPkId); System.out.println(teacher.getName()); }}第三步,數據庫訪問層以及業務邏輯層測試代碼@RunWith(MockitoJUnitRunner.class)public class StudentDaoTest { @Mock private StudentDao studentDao;//模擬對象,不會在jvm中創建真實對象 @Test public void test() throws Exception { Student student = new Student(1L, "張三", Short.valueOf("1"), "湖南省長沙市", 31); //模擬調用,返回對應值,不會發生實際調用 when(studentDao.findByID(1L)).thenReturn(student); //此時會按照模擬預期返回 Student result = studentDao.findByID(1L); assertEquals(result.getName(), "張三"); }}@RunWith(MockitoJUnitRunner.class)public class ClassInfoServiceTest { @Mock private StudentDao studentDao; @Mock private TeacherDao teacherDao; private ClassInfoService actualClassInfoService = new ClassInfoService(); /** * 真實對象中包含部分模擬調用,比如要測ClassInfoService.queryInfo(); * ClassInfoService不能為模擬對象,要實際調用邏輯,但是方法中的對象是模擬出來的, * 以下為模擬方法 * @throws Exception */ @Test public void test() throws Exception { //actualClassInfoService 是通過new 創建的,在jvm中實際存在 //1、通過actualClassInfoService暴露的setXXX 方法傳入模擬的對象 actualClassInfoService.setStudentDao(studentDao); actualClassInfoService.setTeacherDao(teacherDao); Student student = new Student(6L, "張三", Short.valueOf("1"), "湖南省長沙市", 31); //2、設置模擬對象的返回值(如果有),讓業務邏輯按預設的流程進行調用 when(studentDao.findByID(6L)).thenReturn(student); //3、同樣設置其它模擬的對象(如果不需要模擬對象,請傳入實際對象) Teacher teacher = new Teacher(6L, "楊1", 43, Short.valueOf("2")); when(teacherDao.findById(6L)).thenReturn(teacher); //4、使用實際對象調用業務邏輯,驗證真實邏輯 actualClassInfoService.queryInfo(); }}總結,在使用mockito的過程中一定要注意mock的使用場景。如果想測試真實業務邏輯,請不要使用@mock注解,推薦使用@autowired等spring注解,否則會發生不符合期望的結果。實際情況中,mock對象和真實對象存在取舍關系,比如在一段邏輯中,只需要模擬其中一個對象,其它引用的第三方對象均已注冊在springcontext中,因此按需使用mock技術,可以達到事半功倍的作用。mockito技術遠比上述介紹的復雜,也更專業,本文起拋磚引玉的作用,想更深入的掌握mock技術,可以移步mockito的官網(http://mockito.org)學習。更多單元測試技術以及mock方法,可以掃描公眾號交流。
總結
以上是生活随笔為你收集整理的mockito mock void方法_纯干货,浅谈Mockito在单元测试中的实际应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 千兆路由器怎么设置网速最快_200M的网
- 下一篇: python supervisor_py