We are using following most commonly used five annotations in our Selenium WebDriver test scripts:
Test methods must be annotated by the @Test annotation. If the situation requires it, it is also possible to define a method to execute before or after of the test methods with the @Before or @After and @BeforeClass or @AfterClass annotations.
Let’s elaborate on these JUnit annotations here:
The annotation @Test identifies that a method is a test method. When it is mentioned as @Test before a test method, it tells JUnit framework that the following is a Test Method.
The @Before annotation is used to identify the method which is executed before executing the Test Method. This method can be used to set up the test environment.
The @After annotation is method which is executed after executing the Test Method. This method can be used to do teardown i.e. deleting temporary data or setting up default values or cleaning up test environment etc.
The @BeforeClass method is used only once before start all tests. Basically this is used to perform time intensive activities, like connect to a database.
The @AfterClass method is used only once after finish executing all tests. Basically this is used to perform clean-up activities, like disconnect from a database.