You smiled and talked to me of nothing and I felt that for this I had been waiting long.

          The trees come up to my window like the yearning voice of the dumb earth.

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            19 Posts :: 10 Stories :: 25 Comments :: 0 Trackbacks
          最近想玩玩JMock。
          對著官方文檔,想開始寫個test case,不過讓我郁悶的是官方文檔上給的實例代碼不完全。算了,自己寫個跑跑看了。
          1.測試接口:
          IHelloService:
          1 public interface IHelloService {
          2 
          3     /**
          4      * @param name
          5      * @return hello message
          6      */
          7     String sayHelloToSomebody(String name);
          8 
          9 }
          IMPL:
           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:
           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:
           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:
           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...
          版權聲明:轉載時請以超鏈接形式標明文章原始出處和作者信息及本聲明
          posted on 2008-06-20 15:52 鉤子 閱讀(2287) 評論(1)  編輯  收藏 所屬分類: opensources

          Feedback

          # re: jmock2:getting started with junit4 2013-05-06 13:20 abin
          寫的很不錯的,好文,贊一個  回復  更多評論
            

          主站蜘蛛池模板: 齐河县| 翁牛特旗| 天门市| 南投县| 阳西县| 丰县| 昌黎县| 榕江县| 新竹县| 泗洪县| 佛山市| 吉木萨尔县| 紫金县| 灵台县| 开鲁县| 扎赉特旗| 文水县| 山东| 富民县| 边坝县| 出国| 垫江县| 蓬溪县| 枣庄市| 珲春市| 长垣县| 句容市| 肃北| 宜良县| 穆棱市| 彭水| 乐山市| 页游| 安远县| 罗城| 赤峰市| 新干县| 宜州市| 曲松县| 革吉县| 肃宁县|