<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
    寫的很不錯的,好文,贊一個  回復  更多評論
      

    主站蜘蛛池模板: 亚洲欧洲免费无码| 无人在线直播免费观看| 亚洲国产精品成人AV无码久久综合影院| 亚洲视频一区二区三区| 午夜网站在线观看免费完整高清观看| 亚洲精品综合久久| 日韩毛片免费一二三| 亚洲国产精品激情在线观看 | 亚洲国产午夜福利在线播放 | 亚洲乱码无限2021芒果| 4虎永免费最新永久免费地址| 亚洲午夜在线电影| 亚洲一区二区三区免费观看| 亚洲成人网在线观看| 无码专区永久免费AV网站| 亚洲人成色777777精品| 国产免费av片在线无码免费看| 精品一区二区三区无码免费直播| 免费在线观看a级毛片| 久青草视频在线观看免费| 亚洲国产精品第一区二区| 中字幕视频在线永久在线观看免费 | 毛片免费在线观看| 亚洲精品福利在线观看| 18禁成年无码免费网站无遮挡| 亚洲成a人无码亚洲成www牛牛| 国产18禁黄网站免费观看| 成人免费777777被爆出| 亚洲综合成人网在线观看| 成年人免费视频观看| 无遮挡a级毛片免费看| 久久久久久久尹人综合网亚洲| 亚洲成人免费电影| 精品亚洲福利一区二区| 亚洲国产精品一区二区久久hs| 亚洲美女视频免费| 阿v视频免费在线观看| 亚洲国产人成在线观看69网站 | 免费人成网站永久| 亚洲综合精品香蕉久久网97| 国产免费69成人精品视频|