48. Using @BeforeClass and @AfterClass JUnit Annotations





As we've already discussed the advantage of  @BeforeClass over the @Before JUnit Annotation and also the advantage of @AfterClass over the @After JUnit annotation. Lets implement the @BeforeClass and @AfterClass annotations.

As part of Refactoring the Selenium Automation Code we've to use @BeforeClass and @AfterClass JUnit annotations instead of @Before and @After JUnit annotation.

Lets Implement This:

1. Lets view the code containing the @Before and @After JUnit annotations as shown below:



2. Replace the @Before JUnit annotation with @BeforeClass JUnit annotation as shown below:




3. Replace the @After JUnit annotation with @AfterClass JUnit annotation as shown below:



4. Save and Run the 'Class15.java' file by selecting the 'JUnit Test' option
5. Observe that the Selenium Automation Test failed and resulted in the error message "Method setUp() should be static " as shown below:




6. As only the static methods inside the class can be accessible outside the class without creating an object for the class, and as @BeforeClass tries to access the setUp( ) method inside the class before executing the class by trying to access it without creating an object. We've to specify the setUp( ) method specified with @BeforeClass JUnit annotation as static type as shown below:



7. Observe that we got few more error message on changing the setUp( ) method to static type as shown below:



8. The reason for these error messages is 'Static Methods can only access Static type Variables and Static type objects'. In this example,
_driver is not a static type and is not accessible to the setUp( ) method after changing it to static type method. In order to resolve this errors, change the Selenium WebDriver object _driver to 'static' type as shown below and ensure that all the error messages got resolved:



9. Run the 'Class15.java' file by selecting the 'JUnit Test' option again

10. Observe that the Selenium Automation Test failed and resulted in the error message "Method tearDown() should be static " as shown below:



11. As only the static methods inside the class can be accessible outside the class without creating an object for the class, and as @AfterClass tries to access the tearDown( ) method inside the class after executing the class by trying to access it without creating an object. We've to specify the tearDown( ) method specified with @AfterClass JUnit annotation as static type as shown below:




12. Save and Run the 'Class15.java' file by selecting the 'JUnit Test' option once again
13. Observe that the test has passed this time and also the Test Browser has launched only once and closed only once as shown in the below video:

Click Here to watch the Video

Download this Project:


Click here to download this project and import into Eclipse IDE  on your machine.

So, Are we done with Refactoring the code ?

Not yet. We've to still make the code readable by replacing the method names with readable names.

Refactoring the code to make it readable will be explained in the next post.

Please comment below to feedback or ask questions.

Refactoring the Selenium Automation code to make it readable will be explained in the next post.


Followers

Labels