jmock2:getting started with junit4

本文详细介绍了如何使用 JMock 进行单元测试,包括接口定义、实现类、测试类的创建以及如何设置预期行为和验证结果,通过两个具体的测试案例展示了 JMock 在测试接口方法时的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近想玩玩JMock。
对着官方文档,想开始写个test case,不过让我郁闷的是官方文档上给的实例代码不完全。算了,自己写个跑跑看了。
1.测试接口:
IHelloService:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> 1  public   interface  IHelloService {
2 
3       /**
4       *  @param  name
5       *  @return  hello message
6        */
7      String sayHelloToSomebody(String name);
8 
9  }
IMPL:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  1  public   class  HelloServiceImpl  implements  IHelloService {
 2 
 3       /*
 4       * (non-Javadoc)
 5       * 
 6       * @see org.hook.jmock.firstcase.HelloService#sayHelloToSomebody(java.lang.String,
 7       *      java.lang.String)
 8        */
 9       public  String sayHelloToSomebody(String name) {
10           return   " HELLO, "   +  name  +   " ! " ;
11      }
12 
13  }
Test Case:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  1  public   class  IHelloServiceTest  extends  TestCase {
 2       private  Mockery context  =   new  JUnit4Mockery();
 3       private  IHelloService helloService;
 4 
 5       /**
 6       *  @throws  java.lang.Exception
 7        */
 8      @Before
 9       public   void  setUp()  throws  Exception {
10           //  set up
11          helloService  =  context.mock(IHelloService. class );
12      }
13 
14       /**
15       * Test method for
16       * { @link  org.hook.jmock.firstcase.HelloServiceImpl#sayHelloToSomebody(java.lang.String)}.
17        */
18      @Test
19       public   void  testSayHelloToSomebody() {
20           final  String message  =   " HELLO,alex! " ;
21           final  String name  =   " alex " ;
22           //  expectations
23          context.checking(new Expectations() {
24             {
25                 one(helloService).sayHelloToSomebody(name);
26                 will(returnValue(message));
27             }
28 
        });

29           //  execute
30          String result  =  helloService.sayHelloToSomebody(name);
31           //  verify
32          context.assertIsSatisfied();
33          assertSame(result, message);
34      }
35  }
OK,跑下测试,green bar...
2.测试类:
HelloService:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  1  public   class  HelloService {
 2 
 3       /**
 4       *  @param  name
 5       *  @return  hello message
 6        */
 7       public  String sayHelloToSomebody(String name) {
 8           return   " HELLO, "   +  name  +   " ! " ;
 9      }
10  }
Test Case:
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->  1  public   class  HelloServiceTest  extends  TestCase {
 2       private  Mockery context;
 3       private  HelloService helloService;
 4 
 5       /**
 6       *  @throws  java.lang.Exception
 7        */
 8      @Before
 9       public   void  setUp()  throws  Exception {
10          context  =   new  JUnit4Mockery();
11           //  声明针对类进行mock,针对接口则会采用动态代理,不需要声明
12          context.setImposteriser(ClassImposteriser.INSTANCE);
13          helloService  =  context.mock(HelloService. class );
14      }
15 
16       /**
17       * Test method for
18       * { @link  org.hook.jmock.firstcase.HelloService#sayHelloToSomebody(java.lang.String)}.
19        */
20      @Test
21       public   void  testSayHelloToSomebody() {
22           final  String message  =   " HELLO,vivian! " ;
23           final  String name  =   " vivian " ;
24           //  expectations
25          context.checking( new  Expectations() {
26              {
27                  one(helloService).sayHelloToSomebody(name);
28                  will(returnValue(message));
29              }
30          });
31           //  execute
32          String result  =  helloService.sayHelloToSomebody(name);
33           //  verify
34          context.assertIsSatisfied();
35          assertSame(result, message);
36      }
37  }
OK,跑下测试,green bar again...
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
Link: http://www.blogjava.net/alex0927/archive/2008/06/20/209474.html 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值