Junit Annotations

Price: INR 8,999.00
Price: INR 15,999.00
Was: INR 16,999.00

Let’s learn about JUnit Annotations. JUnit is basically a Java Unit Testing framework which is used to create repeatable tests. It is basically a library wrapped up in a jar files.

There are many annotations in JUnit 4 which makes automation easy. It’s easy to create test scripts with the help of annotations. We are going to explain each annotation with the example. Please note the below-mentioned annotations-

 

1. @Test Annotation – You need to keep your test method within the @Test annotation. This annotation tells JUnit that this is the Test method. To execute your test the method you don’t need the main() method. You just need to declare @Test annotation before your test method and @Test annotation tells the JUnit to first make a fresh instance of the class and then run the method to which it is attached.

The method under @Test annotation must be public and void and this method must contain our test case. The test method must contain the word Test. It’s a good practice. If you add Test word in after your test method name then they will execute in order as they declare otherwise they execute randomly. In below example, we are executing two test methods.

 

2. @Ignore Annotation – This annotation tells the JUnit to skip the test method to which it is attached. If attached with @Test annotation, that method will not execute, and if attached to the class which contains the @Test methods, then the whole class will be skipped.

So if you want to ignore the test method then add @Ignore before the test method. You can see the skipped test in Test Report.

Skip Junit Test

3. @Before Annotation – This annotation tells the JUnit to run the method (to which it is attached) to run before each and every method which is attached with @Test annotation.
The before annotation is generally used when we need to initiate the instance variable with the same value every time before executing the test method. You can declare @Before method anywhere in your test class.

Output:

Hi, I am in before annotation
Hi, I am third test
Hi, I am in before annotation
Hi, I am first Test

4. @After Annotation – This annotation tells the JUnit to run the method (to which it is attached) to run after each and every method which is attached with @Test annotation.

 

5. @BeforeClass Annotation – Method declare within @Beforeclass annotation is called before all the test methods in the class. The method attached to this annotation should be public static void (no argument). Example – Suppose you want to open your DB connection before start test execution then you can use this annotation and write DB invoke code inside this method.

 

6. @AfterClass Annotation – Method declare within @Afterclass annotation is called after all the test methods in the class. The method attached to this annotation should be public static void (no argument) .

Example – Suppose you want to close your DB connection after completion of test execution then you can use this annotation and write DB shutdown code inside this method.

Output:

Hi, I am in BeforeClass
Hi, I am in before annotation
Hi, I am third test
Hi, I am in after annotation
Hi, I am in before annotation
Hi, I am first Test
Hi, I am in after annotation
Hi, I am in AfterClass

In the above example, we can see, the @BeforeClass method called first then the @Before method called before each and every @Test method and @After is called after each and every @Test method, @AfterClass method after all the test method. Now you can imagine how easy to design the script with JUnit framework.

 

There are many Annotation as well like @SuiteClasses, @Run, @Rule, @RunWith. We will discuss all these in details in coming tutorials.

 

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply