Saturday 10 September 2016

What is Junit Annotations ?

Junit Annotations are very useful for defining the flow of executions and identification of test methods. These Annotations are used with class and method and it gives additional meaning to the method and class indicating its behavior are flow of executions. For example @Test annotations is used with any Java method and now the java method is identified as a Test Case and it will execute as a Test Case and not like ordinary Java Method. Similarly there are few Basic Junit Annotations which are very important and widely used by Automation Tester.

Consider below Sample Code Example - 
The below code skeleton makes use of Basic Junit Annotations. In below class there are total 6 methods out of which 2 are test cases. Let us see in more details about each annotations.


Public void TestClass
{

@BeforeClass
public void startUp()
{
 your code
}

@Before
public void setUpTest()
{
Your Code
}

@Test
public void firstTest()
{
Your Code
}

@Test
public void secondTest()
{
Your Code
}

@After
public void teatDownTest()
{
Your Code
}

@AfterClass
public void End()
{
Your Code
}

}


@BeforeClass  - In above example this annotations is used for startUp() method indications that as soon as class starts its execution the method with @BeforeClass Annotation will execute first. Any logic which you want to execute first before execution of any other method and test case in the class then add that logic in startUp() method with @BeforeClass Annotation.

@Before - Any method marked with  @Before annotation will execute once before each test case execution. This means setUpTest() method in above execution will execute two times before firstTest() and secondTest(). Any logic which you want to execute before each test case such as launching application etc we need to write it using @Before Annotation.

@Test -  This annotation define that execute firstTest() and secondTest() method as Test Case and not as Method in Java Class.

@After - Any method marked with  @After annotation will execute once After each test case execution. This means tearDownTest() method in above execution will execute two times after firstTest() and secondTest(). Any logic which you want to execute after each test case such as closing the application etc we need to write it using @After Annotation.
@AfterClass - In above example this annotations is used for End() method indications that as soon as all the test cases in class finish execution the method with @AfterClass Annotation will execute. Any logic which you want to execute first at the end of execution of all test cases in the class then add that logic in End() method with @AfterClass Annotation.


The Complete Flow of Executions As per Above Example -

1. startUp()
2. setUpTest()
3. firstTest()
4. teardownTest()
5. setUpTest()
6. secondTest()
7. tearDownTest()
8. End()


1 comment:

  1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here.
    Kindly keep blogging. If anyone wants to become a Java developer learn from Java EE Online Training from India.
    or learn thru Java EE Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete