Junit 4 Core Concept [1] Annotations

All contents of the article are extracted from the book JUnit In Action, 2nd edition by Petar Tahchiev etc.


The requirements to define a test class 

are that the class must be public and contain a zero-argument constructor.


The requirements to create a test method 

are that it must be annotated with@Test, be public, take no arguments, and return void.


Independecy of Each Test Method

JUnit creates a new instance of the test class before invoking each @Test method.This helps provide independence between test methods and avoids unintentional side effects in the test code. Because each test method runs on a new test class instance,we can’t reuse instance variable values across test methods

@Before and @After Annotation

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.

 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.

@BeforeClass and @AfterClass annotations

the @BeforeClass and @AfterClass annotations to annotate your methods in that class. The methods that you annotate will get executed, only once, before/after all of your @Test methods. you can have as many of these methods as you want, and again the order of the execution is unspecified. 

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值