<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 鉤子 閱讀(2302) 評論(1)  編輯  收藏 所屬分類: opensources

    Feedback

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

    主站蜘蛛池模板: 深夜a级毛片免费视频| 亚洲国产二区三区久久| 久久精品国产亚洲AV久| 24小时在线免费视频| 亚洲人成人77777网站| eeuss影院ss奇兵免费com| 亚洲精品第一国产综合精品99| 日韩亚洲翔田千里在线| 24小时免费直播在线观看| 九九精品免费视频| 亚洲国产成人久久综合一区| 成人免费视频网站www| 国产成人精品日本亚洲专区6| 成人免费在线观看网站| 精品久久久久久亚洲中文字幕| 国产91在线免费| 亚洲AV无码乱码在线观看富二代| 十八禁在线观看视频播放免费| 国产av天堂亚洲国产av天堂| 97在线视频免费播放| 亚洲videos| 日韩精品电影一区亚洲| 羞羞视频免费网站在线看| 午夜免费福利影院| 免费国产高清毛不卡片基地| 亚洲国产AV无码专区亚洲AV| 国产成人精品久久免费动漫 | 久久青草亚洲AV无码麻豆| 国产成人无码区免费网站| 亚洲国产超清无码专区| 美女在线视频观看影院免费天天看| 亚洲第一福利网站| 成年女人毛片免费播放视频m| 看全免费的一级毛片| 亚洲VA中文字幕不卡无码| 国产美女在线精品免费观看| 一区二区三区AV高清免费波多| 久久精品国产亚洲AV网站| 久久久久国色AV免费观看性色| 国产免费一区二区三区免费视频 | 日韩色视频一区二区三区亚洲 |