????????Mock??Stub??????
???????????? ???????[ 2013/10/18 10:13:27 ] ????????
?????????????????????????????????????????д??????????????????????????????????????????????????ɡ?????????????????????????????????????????????????
??????????????????Mock/Stub??????????
????????????????????????Mock/Stub??????????????????Щ????????????????????????????????????????Щ???????????????????????class?? ??class BlogDao?? ??????????????????????????class ConfigReader?? ??????????????????????????????class MyStringParser?? ??????????????????к??????????????ж?????????????????????????????????????????????????????????????????磬???????class MyStringProcessor?? ???????MyStringParser?? ???????MyStringProcessor ???????????????MyStringParser ?????????????????????????????????MyStringParser ????????????????????????????????????????????????????????У??????????????????ζ?????????????????????????????ζ?????????????????????????????????????????“?????????????”???ж??????????????????
?????????????????????Mock/Stub???????????????????????????????????????????????????????
????Back to Mock/Stub?? ????????????У????????Mock???????????????????“????”?? “????”????????“????”??“????”???????? ????“????”???????Щ????“????”???????????Mock?????????????????????????????????????????????????????Stub / Mock objects??????????????Stub / Mock ????????????????Stub / Mock ??????а??????????????????????????????“?????????”??“?????????”????????????????д???е????????????????????
????????????????????Stub / Mock????????????????磬?? xUnit Test Patterns ?????????????????????Test Double?????stunt double ???????????????????????????????
???? · Dummy Object
???? · Test Stub
???? · Test Spy
???? · Mock Object
???? · Fake Object
???????У?Fake Object????????????????????????????????????????磬 InMemoryBlogDao??????? SqlBlogDao???????????????????????????? BlogDao ??? ??????????
??????????????????????Dummy Object??Test Stub??Test Spy??????????Stub????μ?Mock Object ?????Mock??
?????????Dummy Object????????????Dummy Object???????????????????????????????????????????????п???????ò??????????磬????BlogService ??? BlogDao????????????BlogService ?????????????????????BlogDao?????????????? new BlogService(new NullBlogDao())?? NullBlogDao ??Dummy Object?????????????????????????????????????????
????Test Stub ???????? ??????????????ε??????????????????????????????ò???????????????????????????????????Щ??????????磬???????HttpContextStub ????????????HttpContext?? ??????????SessionId?? ResquestParameter ??????????????????????Щ??????????????????getSessionId() ??????????????????????????ε????????????????
????Test Spy ???????????????????????????????????????BlogService ????????????????TestSpy??
public class BlogDaoTestSpy implements BlogDao {
public Blog savedBlog = null;
public void save(Blog blog) {
savedBlog = blog;
}
}
??????????д????????
@Test
public void testSaveBlog(){
Blog blog = new Blog();
blogService.save(blog);
assertEqual(blog?? ((BlogDaoTestSpy)blogDao).savedBlog);
}
???????????????????????blogService.save(blog) ????blogDao??savedBlog ?????仯?????????????blogDao??????????????????????????仯???
??????
???·???
??????????????????
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