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

    Feedback

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

    主站蜘蛛池模板: 精品亚洲成a人片在线观看少妇 | 和老外3p爽粗大免费视频| 中文字幕亚洲无线码a| 18pao国产成视频永久免费| 色偷偷尼玛图亚洲综合| 国产亚洲精品va在线| 四虎国产精品免费久久| 精品免费久久久久国产一区| 亚洲欧洲在线播放| 国产成人精品曰本亚洲79ren| 国产精品免费观看调教网| 丰满亚洲大尺度无码无码专线| 国产av无码专区亚洲av桃花庵| 免费人成在线视频| 久久精品免费观看| 国产亚洲男人的天堂在线观看| 91久久亚洲国产成人精品性色| 亚洲国产成人久久综合碰| 欧洲乱码伦视频免费| 91在线视频免费观看| 久久久亚洲精华液精华液精华液| 久久久久亚洲AV成人无码| 免费在线观看黄网| 性做久久久久久久免费看| 久草免费福利资源站| 深夜特黄a级毛片免费播放| 亚洲国产成人va在线观看网址| 亚洲真人无码永久在线| 免费无码黄网站在线观看| 最近中文字幕免费mv在线视频| 国产黄在线播放免费观看| 亚洲一区二区三区在线观看网站| 亚洲一区二区三区四区在线观看 | 亚洲精品乱码久久久久久蜜桃不卡| 欧美三级在线电影免费| 99久9在线|免费| a毛片全部免费播放| 日韩少妇内射免费播放| 亚洲AV成人精品日韩一区| 国产v亚洲v天堂a无| 亚洲激情黄色小说|