首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

easymock课程-创建stub对象

2012-06-30 
easymock教程-创建stub对象??? 前面教程中有个章节讨论到mock和stub的概念差别,一般来说easymock如其名所

easymock教程-创建stub对象


??? 前面教程中有个章节讨论到mock和stub的概念差别,一般来说easymock如其名所示,主要是用来做mock用的,但是easymock中也提供有对stub的支持, 主要体现在andStubAnswer(),andStubDelegateTo(),andStubReturn(),andStubThrow()和asStub()等方法的使用上。

?

??? 我们来看一个实际使用的例子:

?

easymock课程-创建stub对象easymock课程-创建stub对象????public?class?Business?{
easymock课程-创建stub对象????????private?Service?????service;
easymock课程-创建stub对象
easymock课程-创建stub对象????????private?StubService?stubService;
easymock课程-创建stub对象
easymock课程-创建stub对象easymock课程-创建stub对象????????public?void?execute1()?{
easymock课程-创建stub对象easymock课程-创建stub对象????????????if?(service.execute())?{
easymock课程-创建stub对象????????????????stubService.method1();
easymock课程-创建stub对象????????????}
easymock课程-创建stub对象????????}
easymock课程-创建stub对象
easymock课程-创建stub对象easymock课程-创建stub对象????????public?void?execute2()?{
easymock课程-创建stub对象easymock课程-创建stub对象????????????if?(service.execute())?{
easymock课程-创建stub对象????????????????stubService.method2();
easymock课程-创建stub对象????????????}
easymock课程-创建stub对象????????}
easymock课程-创建stub对象
easymock课程-创建stub对象easymock课程-创建stub对象????????public?void?execute3()?{
easymock课程-创建stub对象easymock课程-创建stub对象????????????if?(service.execute())?{
easymock课程-创建stub对象????????????????stubService.method1();
easymock课程-创建stub对象????????????????stubService.method2();
easymock课程-创建stub对象????????????}
easymock课程-创建stub对象????????}
easymock课程-创建stub对象
easymock课程-创建stub对象easymock课程-创建stub对象????????public?void?setStubService(StubService?stubService)?{
easymock课程-创建stub对象????????????this.stubService?=?stubService;
easymock课程-创建stub对象????????}
easymock课程-创建stub对象
easymock课程-创建stub对象easymock课程-创建stub对象????????public?void?setService(Service?service)?{
easymock课程-创建stub对象????????????this.service?=?service;
easymock课程-创建stub对象????????}
easymock课程-创建stub对象????}
easymock课程-创建stub对象
easymock课程-创建stub对象easymock课程-创建stub对象????private?interface?Service?{
easymock课程-创建stub对象????????public?boolean?execute();
easymock课程-创建stub对象????}
easymock课程-创建stub对象
easymock课程-创建stub对象easymock课程-创建stub对象????private?interface?StubService?{
easymock课程-创建stub对象
easymock课程-创建stub对象????????public?String?method1();
easymock课程-创建stub对象
easymock课程-创建stub对象????????public?String?method2();
easymock课程-创建stub对象????}
easymock课程-创建stub对象

??? 这里的Business类依赖到Service和StubService,execute1() / execute2() / execute3() 是我们需要测试的三个方法,相同点都是必须调用service.execute(),不同点在于其后对stubService的调用各不相同。而我们假设在这里我们只关心Business类对Service的调用是否如预期,不关心对于StubService的调用,只要程序可以继续运行就可以了。

?

??? 一个正统的做法是手工写一个StubService的stub 实现,例如:

easymock课程-创建stub对象easymock课程-创建stub对象private?class?StubServiceImpl?{
easymock课程-创建stub对象easymock课程-创建stub对象?public?String?method1()?{
easymock课程-创建stub对象??return?"";
easymock课程-创建stub对象?}
easymock课程-创建stub对象
easymock课程-创建stub对象easymock课程-创建stub对象?public?String?method2()?{
easymock课程-创建stub对象??return?"";
easymock课程-创建stub对象?}
easymock课程-创建stub对象}
easymock课程-创建stub对象
easymock课程-创建stub对象

??? 但是如果这个接口复杂方法众多,则这个stub类不得不实现所有的其他方法,即使完全用不到,因为java的语法限制。

?

??? 可以这样使用easymock来实现stub:

?

easymock课程-创建stub对象easymock课程-创建stub对象public?class?BusinessTest?{
easymock课程-创建stub对象
easymock课程-创建stub对象????private?Business??????business;
easymock课程-创建stub对象
easymock课程-创建stub对象????private?IMocksControl?mocksControl;
easymock课程-创建stub对象
easymock课程-创建stub对象????private?Service???????service;
easymock课程-创建stub对象
easymock课程-创建stub对象????@Before
easymock课程-创建stub对象easymock课程-创建stub对象????public?void?init()?{
easymock课程-创建stub对象????????business?=?new?Business();
easymock课程-创建stub对象????????business.setStubService(prepareStubService());
easymock课程-创建stub对象????????mocksControl?=?EasyMock.createStrictControl();
easymock课程-创建stub对象????????service?=?mocksControl.createMock("service",?Service.class);
easymock课程-创建stub对象????????business.setService(service);
easymock课程-创建stub对象????}
easymock课程-创建stub对象
easymock课程-创建stub对象easymock课程-创建stub对象????private?StubService?prepareStubService()?{
easymock课程-创建stub对象????????StubService?service?=?EasyMock.createMock("stubService",?StubService.class);
easymock课程-创建stub对象????????service.method1();
easymock课程-创建stub对象????????EasyMock.expectLastCall().andStubReturn("");
easymock课程-创建stub对象????????service.method2();
easymock课程-创建stub对象????????EasyMock.expectLastCall().andStubReturn("");
easymock课程-创建stub对象????????EasyMock.replay(service);
easymock课程-创建stub对象????????return?service;
easymock课程-创建stub对象????}
easymock课程-创建stub对象
easymock课程-创建stub对象????@Test
easymock课程-创建stub对象easymock课程-创建stub对象????public?void?testExecute1()?{
easymock课程-创建stub对象????????EasyMock.expect(service.execute()).andReturn(true);
easymock课程-创建stub对象????????mocksControl.replay();
easymock课程-创建stub对象????????business.execute1();
easymock课程-创建stub对象????????mocksControl.verify();
easymock课程-创建stub对象????}
easymock课程-创建stub对象
easymock课程-创建stub对象????@Test
easymock课程-创建stub对象easymock课程-创建stub对象????public?void?testExecute2()?{
easymock课程-创建stub对象????????EasyMock.expect(service.execute()).andReturn(false);
easymock课程-创建stub对象????????mocksControl.replay();
easymock课程-创建stub对象????????business.execute2();
easymock课程-创建stub对象????????mocksControl.verify();
easymock课程-创建stub对象????}
easymock课程-创建stub对象
easymock课程-创建stub对象????@Test
easymock课程-创建stub对象easymock课程-创建stub对象????public?void?testExecute3()?{
easymock课程-创建stub对象????????EasyMock.expect(service.execute()).andReturn(true);
easymock课程-创建stub对象????????mocksControl.replay();
easymock课程-创建stub对象????????business.execute3();
easymock课程-创建stub对象????????mocksControl.verify();
easymock课程-创建stub对象????}
easymock课程-创建stub对象}

?

??? 在方法prepareStubService()中,我们通过easymock创建了一个mock对象,然后和普通mock对象一样记录了期望的行为。不同的是用andStubReturn()替代了andReturn().

?

??? 然后我们直接调用EasyMock.replay(service),注意在这个测试案例中,我们另外创建了一个mocksControl并通过这个mocksControl创建了我们关注的Service接口的mock对象,它的record/replay/和verify()是和StubService完全分离的。这样做的好处是在execute1() / execute2() / execute3()的测试案例中,我们完全不必额外关心这个stub,所有的事情在init()函数中就已经准备好了。这样做的好处显而易见,execute1() / execute2() / execute3()的测试案例中,代码和测试逻辑都简单了。

?

??? 最后总结,在适当的时候使用easymock来创建stub对象,对于简化测试还是能有所帮助的。

热点排行