Annotations in JUnit

来源:互联网 发布:招聘node 编辑:程序博客网 时间:2024/05/18 03:02

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.