`
akunamotata
  • 浏览: 374515 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

junit in action 2nd note

    博客分类:
  • note
阅读更多

The @Before and @After annotated methods are executed right before/after the
execution of each one of your @Test methods and regardless of whether the test
failed or not. This helps you to extract all of your common logic, like instantiating
your domain objects and setting them up in some known state. You can have as
many of these methods as you want, but beware that if you have more than one of
the @Before/@After methods, the order of their execution is not defined.

JUnit also provides the  @BeforeClass and  @AfterClass annotations to anno-
tate your methods in that class. The methods that you annotate will get exe-
cuted, only once, before/after all of your  @Test methods. Again, as with the
@Before and  @After annotations, you can have as many of these methods as
you want, and again the order of the execution is unspecified.

both the @Before/@After and @BeforeClass/@After-
Class annotated methods must be  public. The  @BeforeClass/@AfterClass
annotated methods must be public and static.


JUnit best practices: unit test one object at a time

JUnit best practices: choose meaningful test method names
a good rule is to start with the testXXX naming scheme, where XXX is the name of
the domain method to test.

JUnit best practices: explain the failure reason in assert calls

JUnit best practices: one unit test equals one @Test method

JUnit best practices: test anything that could possibly fail

JUnit best practices: let the test improve the code

JUnit best practices: make exception tests easy to read

JUnit provides us with another parameter to the @Test annotation
called timeout. In this parameter, you can specify your time barrier in terms of milli-
seconds, and if the test takes more time to execute, JUnit will mark the test as failed.

it’s good to skip some of the tests."@Ignore"

JUnit best practice: always specify a reason for skipping a test

In JUnit 4, when you annotate methods with @Ignore, you
get statistics that include how many tests JUnit skipped in addition to how many tests
passed and failed.

JUnit best practices: same package, separate directories

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics