In Java, a special form of syntactic metadata can be added to Java source code, which is known as Annotations. Variables, parameters, packages, methods, and classes are annotated. Some of the JUnit Annotations are:
- @Test: The annotation @Test finds that a method is a test method. When used before a test method, it is mentioned as @Test; it informs the JUnit framework that the following method is a test method.
- @Before: The @Before annotation is used to find the method which is executed before executing the test method. This method can be used to set up the test environment.
- @After: The @After annotation is a method that is executed after executing the Test Method. This method can be used to do teardown, i.e., it is a method used to delete all temporary data, set up default values, clean up test environment, etc.
- @Ignore: The @Ignore test annotation is used to ignore particular tests or group of tests in order to skip the build failure.
- @BeforeClass: The @BeforeClass method is used only once before the start of all tests. Basically, this is used to perform cumbersome activities, like connecting to a database.
- @AfterClass: The @AfterClass method is used only once after executing all tests. Basically, this is used to carry out clean-up activities, like disconnecting from a database.
- @RunWith: A JUnit Runner is class that extends JUnit's abstract Runner class. Runners are used for running test classes. The Runner that should be used to run a test can be set using the @RunWith annotation.