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

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

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

    jinfeng_wang

    G-G-S,D-D-U!

    BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
      400 Posts :: 0 Stories :: 296 Comments :: 0 Trackbacks

    Junit Test Practices

     

    Now that we've seen JUnit in action, let's step back a little and look at some good practices for writing tests. Although we'll discuss implementing them with JUnit, these practices are applicable to whatever test tool we may choose to use.

     

    Write Tests to Interfaces

    Wherever possible, write tests to interfaces, rather than classes. It's good OO design practice to program to interfaces, rather than classes, and testing should reflect this. Different test suites can easily be created to run the same tests against implementations of an interface (see Inheritance and Testing later).

    對某個類進行測試時,僅測試該類對外公開的接口,適當(dāng)?shù)臏y一些其內(nèi)部的接口。當(dāng)一個類從其他類繼承方法時,那么對這些方法的測試則不應(yīng)該由subclass的測試完成,而由parent class的測試完成。

     

    Don't Bother Testing JavaBean Properties

    It's usually unnecessary to test property getters and setters. It's usually a waste of time to develop such tests. Also, bloating test cases with code that isn't really useful makes them harder to read and maintain.

     

    Maximizing Test Coverage

    Test-first development is the best strategy for ensuring that we maximize test coverage. However, sometimes tools can help to verify that we have met our goals for test coverage. For example, a profiling tool such as Sitraka'sJProbe Profiler (discussed in Chapter 15) can be used to examine the execution path through an application under test and establish what code was (and wasn't) executed. Specialized tools such as JProbe Coverage (also part of theJProbe Suite) make this much easier. Jprobe Coverage can analyze one or more test runs along with the application codebase, to produce a list of methods| and even lines of source code that weren't executed. The modest investment in such a tool is likely to be worthwhile when it's necessary to implement a test suite for code that doesn't already have one.

     

    Don't Rely on the Ordering of Test Cases

    When using reflection to identify test methods to execute, JUnit does not guarantee the order in which it runs tests. Thus tests shouldn't rely on other tests having been executed previously. If ordering is vital, it's possible f to add tests to a TestSuite object programmatically. They will be executed in the order in which they were added. However, it's best to avoid ordering issues by using the setup () method appropriately.

     

    Avoid Side Effects

    For the same reasons, it's important to avoid side effects when testing. A side effect occurs when one test changes the state of the system being tested in a way that may affect subsequent tests. Changes to persistent data in a database are also potential side effects.

     

    Read Test Data from the Classpath, Not the File System

    It's essential that tests are easy to run. A minimum of configuration should be required. A common cause of problems when running a test suite is for tests to read their configuration from the file system. Using absolute file paths will cause problems when code is checked out to a different location; different file location and path conventions (such as \home\rodj \tests\foo.dat or C:\\Documents and Settings\ \rodj \ \ f oo.dat) can tie tests to a particular operating system. These problems can be avoided by loading test data from the classpath, with the Class.getResource () or   Class.getResourceAsStream() methods. The necessary resources are usually best placed in the same directory as the test classes that use them.

     

    Avoid Code Duplication in Test Cases

    Test cases are an important part of the application. As with application code, the more code duplication they contain, the more likely they are to contain errors. The more code test cases contain the more of a chore they are to write and the less likely it is that they will be written. Avoid this problem by a small investment in test infrastructure. We've already seen the use of a private method by several test cases, which greatly simplifies the test methods using it.


     

    Inheritance and Testing

    We need to consider the implications of the inheritance hierarchy of classes we test. A class should pass all tests associated with its superclasses and the interfaces it implements. This is a corollary of the "Liskov Substitution Principle", which we'll meet in Chapter 4.

    When using JUnit, we can use inheritance to our advantage. When one JUnit test case extends another (rather than extending junit.framework.TestCase directly), all the tests in the superclass are executed, as well as tests added in the subclass. This means that JUnit test cases can use an inheritance hierarchy paralleling the concrete inheritance hierarchy of the classes being tested.

    In another use of inheritance among test cases, when a test case is written against an interface, we can make the test case abstract, and test individual implementations in concrete subclasses. The abstract superclass can declare a protected abstract method returning the actual object to be tested, forcing subclasses to implement it.

    It's good practice to subclass a more general JUnit test case to add new tests for a subclass of an

    object or a particular implementation of an interface.

    posted on 2005-04-25 16:22 jinfeng_wang 閱讀(846) 評論(0)  編輯  收藏 所屬分類: ZZJunit
    主站蜘蛛池模板: 亚洲AV无码一区二区二三区入口 | 午夜免费不卡毛片完整版| 亚洲AV人人澡人人爽人人夜夜 | 亚洲日本VA午夜在线电影| 亚洲国产日韩在线成人蜜芽| 精品亚洲永久免费精品| 国产亚洲成av人片在线观看| 99久久成人国产精品免费| 2021免费日韩视频网| 亚洲黄色在线观看| 亚洲一区二区三区免费观看| 亚洲国产成人精品无码区在线秒播 | 亚洲视频在线播放| 猫咪免费人成网站在线观看| 亚洲欧洲综合在线| 最近高清国语中文在线观看免费| 亚洲国产精品自在自线观看| 免费在线观看理论片| 一本到卡二卡三卡免费高 | 国产性爱在线观看亚洲黄色一级片| 久久一区二区免费播放| 亚洲天天在线日亚洲洲精| 男女免费观看在线爽爽爽视频 | 亚洲深深色噜噜狠狠网站| 日本最新免费不卡二区在线| 久香草视频在线观看免费| 国产l精品国产亚洲区在线观看| 3344永久在线观看视频免费首页| 激情亚洲一区国产精品| 日本一道高清不卡免费| 成在人线av无码免费高潮水| 亚洲色图校园春色| 免费a级毛片永久免费| 免费看无码特级毛片| 国产人成亚洲第一网站在线播放| 免费一级毛片不卡在线播放| 精品亚洲永久免费精品| 亚洲AV无码一区二区一二区| 亚洲成av人片天堂网| 日韩免费高清视频网站| 中文字幕日本人妻久久久免费|