junit in action 2nd 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值