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

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

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

    隨筆 - 41  文章 - 29  trackbacks - 0
    <2009年4月>
    2930311234
    567891011
    12131415161718
    19202122232425
    262728293012
    3456789

    常用鏈接

    留言簿(5)

    隨筆分類(28)

    隨筆檔案(23)

    收藏夾(6)

    Inside JVM

    Java

    java performance

    Solr

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    Currently, the major approach for java unit test are built on two major components –

    • Junit – The unit test framework
    • EasyMock and EasyMock Extension – Which are used to build mock test

    What’s problem on Mock Test? It costs a lot time on preparation

    Mock test is a very good way to test unit of codes. I still prefer mock test as the major approach of unit test. However, mock test has its own problem.

    The major steps on Mock Test is –

    1. Assume we are going to test “Output ServiceA.MethodA(Input input)”, firstly, get an instance of ServiceA
    2. Create an Input for methodA, which generally need new an object and invoke many setter methods to set the values
    3. If this method will call ServiceB.MethodB, we need create a mock object for ServiceB, and create an ArugmentMather for methodB to match the arguments, and create a return object for methodB
    4. Then we invoke ServiceA.MethodA and internally ServiceA.MethodA will invoke ServiceB.MethodB, and finally, we get the Output object
    5. At the last step, we will assert the Output object.

    In order to test ServiceA.MethodA, we did lots of things, and most of things are very time-consuming activities, such as

    • creating Input object,
    • creating Mock service,
    • creating ArgumentMacher for mock method, and
    • creating return object for mock method
    • And assert Output object

    What’s my expectation?

    • creating Input object – Can it be created automatically?
    • creating Mock service – Can it be created automatically?
    • creating ArgumentMacher for mock method, and – Can we NOT need create a new class for it?
    • creating return object for mock method – Can it be created automatically?
    • And assert Output object – Instead of equals() method, can we compare two object by values?

    How can Unitils help us?

    Unitils is an open source library aimed at making unit testing easy and maintainable. Unitils builds further on existing libraries like dbunit and integrates with JUnit and TestNG .

    Unitils provides general asserion utilities, support for database testing, support for testing with mock objects and offers integration with Spring , Hibernate and the Java Persistence API (JPA).

    • General testing utilities
      Equality assertion through reflection, with different options like ignoring Java default/null values and ignoring order of collections
    • EasyMock integration support
      • Simplify EasyMock mock object creation
      • Simplify mock object injection
      • EasyMock argument matching using reflection equality

    Back to our problem –

    • Can we initialize a Input or Mock Method Returned Object automatically? In other words, creating an instance of a specific class, and fill all attributes by random values. Unfortunately, Unitils doesn’t have direct support on it. But i’ve created a simple util to initialize an bean based on Unitils.
    1 //for BeanInitializer.initializeSimpleBean,  it is an simple tool to initialize a bean by random values. I didn't find a good tool for it, so, i write a simple one.
    2 LogStatisticRawDataInput input = (LogStatisticRawDataInput) BeanInitializer.initializeSimpleBean(LogStatisticRawDataInput.class);
    • Can a mock object be created automatically? Yes, Unitils provides an annotation ”@ReguarMock” to support it
       1 public class SearchStatisticServiceImplTest extends com.starcite.commonsearch.test.Test {
       2 
       3     
       4 
       5     @RegularMock
       6 
       7     private StatisticAccess mockAccess; //just it
       8 
       9     
      10 
      11     private SearchStatisticService statisticService;
      12 
      13     
      14 
      15     @Before
      16 
      17     public void setUp() {
      18 
      19     statisticService = new SearchStatisticServiceImpl();
      20 
      21     ((SearchStatisticServiceImpl)statisticService).setStatisticAccess(mockAccess);
      22 
      23     }
      24 
      25     
      26 
      27     }
    • Can we compare the expected object and the real object by contents? Of course, no need write the equals by yourself. Yes, Unitils provides “Equality assertion through reflection, with different options like ignoring Java default/null values and ignoring order of collections”
      1     Map<String, Integer> expectedKeywordFreqMap = new HashMap<String, Integer>();
      2 
      3     Map<String, Integer> keywordFreqMap = output.getKeywordFreqMap();
      4     ReflectionAssert.assertReflectionEquals(expectedKeywordFreqMap, keywordFreqMap); //that's it
    • Can we create AugmentMatcher easier? Yes, Unitils provides “EasyMock argument matching using reflection equality”.
       1 //parameter matcher
       2 
       3     StatRawDataImpl rawData = new StatRawDataImpl();
       4 
       5     rawData.setKeyword(input.getKeyword());
       6 
       7     rawData.setLocation(input.getLocation());
       8 
       9     rawData.setAccountID(input.getUserID());
      10 
      11     rawData.setCreatedDate(new Date());
      12 
      13     rawData.setSiteID(input.getSiteID());
      14 
      15     rawData.setUniqueID(input.getSearchID());
      16 
      17 
      18     List<statsearchrank> searchRankList = new ArrayList</statsearchrank><statsearchrank>(); 
      19 
      20     for (int i = 0; i &lt; SEARCH_RESULT_SIZE; i++) {
      21 
      22            Long externalVendorID = searchResults.get(i);
      23 
      24            StatSearchRank rank = new StatSearchRankImpl();
      25 
      26            rank.setRank((input.getPageNum() - 1* //current page number
      27 
      28                               (input.getKeywordLocationPreferredSize() + input.getPageSize()) //total page size
      29 
      30                                                       + i + 1);
      31 
      32             rank.setExternalVendorID(externalVendorID);
      33 
      34             searchRankList.add(rank);
      35 
      36     }
      37 
      38     mockAccess.logStatisticRawData(
      39 
      40                 EasyMockUnitils.refEq(rawData, ReflectionComparatorMode.LENIENT_DATES), //that's it, no need argument matcher
      41 
      42                 EasyMockUnitils.refEq(searchRankList));
      43 
      44     EasyMock.expectLastCall().once();
      45 
      46     EasyMockUnitils.replay();
      47

    Overall, Unitils  is a pretty good tool to simplify unit test codes. Here  we only mentioned Mock test, actually, Unitils can also simplify DB test/Spring Test etc. We will explore these futures later.
    posted on 2009-03-30 22:30 Justin Chen 閱讀(2013) 評論(1)  編輯  收藏 所屬分類: Unit Test & Mock Test

    FeedBack:
    # re: [絕對原創] Use Unitils To Simplify Mock Test  2009-04-13 21:30 Justin Chen
    Today, i took a look at another mock framework - JMockit (https://jmockit.dev.java.net/). The creator of JMockit claims "JMockit Core consists of a single class with a small set of static methods, which allow arbitrary methods and constructors of any other class to be replaced with mock implementations at runtime. "

    It overcomes two limits comparing to EasyMock,
    (1) mocked classes can't simply be instantiated with the new operator in client or test code.
    (2) the classes to be mocked cannot be final

    However, JMockit has its own limitation: the argument matcher provided by JMockit is not easy to use comparing to Unitils.  回復  更多評論
      
    主站蜘蛛池模板: 69视频免费在线观看| 国产自国产自愉自愉免费24区 | 国产精品亚洲综合网站| 91视频国产免费| 亚洲自国产拍揄拍| 免费无码精品黄AV电影| 亚洲一区二区三区高清在线观看| 无码免费午夜福利片在线| 亚洲av成人一区二区三区| 成人免费视频一区| 亚洲av无码日韩av无码网站冲| 波多野结衣久久高清免费| 国产精品亚洲综合一区在线观看| 亚洲精品麻豆av| 免费人成激情视频在线观看冫| 久久久久亚洲Av片无码v| 免费A级毛片无码专区| 亚洲 欧洲 自拍 另类 校园| 日韩精品视频免费在线观看| 一级毛片**免费看试看20分钟 | 亚洲一区二区三区免费在线观看| 亚洲国色天香视频| 日韩在线免费播放| 国产免费A∨在线播放| 亚洲国产日韩一区高清在线| 91黑丝国产线观看免费| 精品亚洲国产成人av| 久久亚洲中文字幕精品一区四 | 无码视频免费一区二三区| 亚洲日韩在线中文字幕综合| 国产午夜亚洲精品午夜鲁丝片| 99久久免费看国产精品| 亚洲av午夜电影在线观看 | 日韩毛片免费无码无毒视频观看| 亚洲国产精华液2020| 91麻豆精品国产自产在线观看亚洲| 最近免费字幕中文大全视频| 亚洲AV无码一区二区一二区| 国产国拍亚洲精品mv在线观看 | 99久久久精品免费观看国产| 国产精品亚洲专区在线播放 |