Example Of Difference Between @Before/@After VS @BeforeClass/@AfterClass In JUnit With WebDriver
Difference between @Before and @BeforeClass annotations
- Test method marked with @Before annotation will be executed before the each @Test method. Means if there are 5 @Test methods in your class then @Before test method will be executed 5 times.
- Test method marked with @BeforeClass annotation will be executed just before the class. Means @BeforeClass method will be executed only once before the class even if there are 5 @Test methods in your class.
Difference between @After and @AfterClass annotations
- Same as @Before annotation, Test method marked with @After annotation will be executed after the each @Test method.
- @AfterClass annotation will be executed only once after last @Test method executed.
Execute bellow given @Before and @After annotations example in your eclipse and observe result in console.
package junitpack; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class junittest2 { private static WebDriver driver;
@Before public void openbrowser() { System.out.print("\nBrowser open"); driver = new FirefoxDriver(); driver.manage().window().maximize(); driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html"); } @After public void closebrowser() { System.out.print("\nBrowser close"); driver.quit(); }
@Test public void test1() throws InterruptedException{ driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest2 class-test1"); System.out.print("\njunittest2 class-test1 method is executed"); Thread.sleep(2000); } @Test public void test2() throws InterruptedException { driver.findElement(By.xpath("//input[@name='fname']")).clear(); driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("junittest2 class-test2"); Thread.sleep(2000); System.out.print("\njunittest2 class-test2 method is executed"); } }
When execute above example in eclipse, Bellow given result will be displayed in console.
Output :
Browser open
junittest2 class-test1 method is executed
Browser close
Browser open
junittest2 class-test2 method is executed
Browser close
Based on above given console output, we can say each @Before method is
executed before each @Test method and each @After method is executed
after each @Test method. Now let we replace @Before annotation
with @BeforeClass and @After annotation with @AfterClass in same example
and then observe result. Replace bellow given @BeforeClass and
@AfterClass part with @Before and @After part in above example as
bellow.
@BeforeClass
public static void openbrowser() {
System.out.print("\nBrowser open");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
}
@AfterClass
public static void closebrowser() {
System.out.print("\nBrowser close");
driver.quit();
}
Now run above example and look at console result. Console result will be as bellow.
Output :
Browser open
junittest2 class-test1 method is executed
junittest2 class-test2 method is executed
Browser close
As per console result, we can say @BeforeClass method is executed once
only. Same way @AfterClass annotation marked method is also executed
once only.