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 首頁 新隨筆 聯(lián)系 聚合 管理
            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         // 聲明針對類進(jìn)行mock,針對接口則會采用動態(tài)代理,不需要聲明
          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...
          版權(quán)聲明:轉(zhuǎn)載時請以超鏈接形式標(biāo)明文章原始出處和作者信息及本聲明
          posted on 2008-06-20 15:52 鉤子 閱讀(2295) 評論(1)  編輯  收藏 所屬分類: opensources

          Feedback

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

          主站蜘蛛池模板: 沐川县| 高雄市| 普洱| 塔城市| 乃东县| 轮台县| 呼和浩特市| 兰州市| 方山县| 安溪县| 昭通市| 天峻县| 松原市| 抚远县| 建宁县| 滨州市| 义乌市| 庐江县| 临汾市| 页游| 泸水县| 北碚区| 洪江市| 涿鹿县| 股票| 连州市| 襄垣县| 三台县| 霍邱县| 旬邑县| 永新县| 资源县| 平泉县| 郎溪县| 栾川县| 米林县| 尼玛县| 静宁县| 称多县| 连南| 定兴县|