Annotations in JUnit

http://www.devmanuals.com/tutorials/java/JavaTestingTools/junit/AnnotationsInJUnit.html

https://github.com/junit-team/junit/wiki/Getting-started

 

Annotations are like meta-tags that you can add to you code and apply them to methods or in class. These annotation in JUnit gives us information about test methods , which methods are going to run before & after test methods, which methods run before & after all the methods, which methods or class will be ignore during execution, which methods are suspected to throw exception etc.

Annotation are added in JUnit 4 version & you can found it in all the releases after this version. Annotations reduces coding & extra burden from tester. The annotations in JUnit are predefined and need not to define, you can implement it directly. Also you can have your own defined annotations in class. But for testing , annotations are predefined in JUnit.

 

Notes

All test methods are annotated with @Test. Unlike JUnit3 tests, you do not need to prefix the method name with "test" (and usually don't)
Do not have your test classes extend junit.framework.TestCase (directly or indirectly). Usually, tests with JUnit4 do not need to extend anything (which is good, since Java does not support multiple inheritance)
Do not use any classes in junit.framework or junit.extensions

 

Given below list of annotations and their meaning in JUnit :

AnnotationDescription

@Test

The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case.

@Before

Several tests need similar objects created before they can run. Annotating a public void method with @Before causes that method to be run before each Test method.

@After

If you allocate external resources in a Before method you need to release them after the test runs. Annotating a public void method with @After causes that method to be run after the Test method.

@BeforeClass

Annotating a public static void method with @BeforeClass causes it to be run once before any of the test methods in the class. This can be used to perform computationally expensive setup (like logging into a database).

@AfterClass

Will perform the method after all tests have finished. This can be used to perform clean-up activities for example be used to disconnect to a database

@Ignore

Sometimes you want to temporarily disable a test or a group of tests. Methods annotated with Test that are also annotated with @Ignore will not be executed as tests. Also, you can annotate a class containing test methods with @Ignore and none of the containing tests will be executed.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值