JUnit5 also provides the support to exclude/disable the tests. The @Disabled annotation in JUnit5 is used to exclude the test methods from the test suite. This annotation can be applied over a test class as well as over individual test methods. This annotation accepts only one optional parameter where we can mention the reason to skip the tests. @Disabled annotation can be used without providing any reason but its always good to provide a reason why this particular test case has been disabled, or issue tracker id for better understanding.
1.Disable Test Methods
In the below example, I have annotated 2 test methods out of 5 test methods as @Disabled with a parameter which specify the reason for disabling the test that means these 2 test methods should not be executed.
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class DisabledTestsDemo {
WebDriver driver;
@BeforeEach
public void setUp() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
driver = new ChromeDriver(chromeOptions);
driver.get("https://duckduckgo.com/");
}
@Disabled("This test is blocked till bug 1290 is fixed")
@Test
void verifyBrowserText() {
boolean displayed = driver.findElement(By.xpath("//section[@class='homepage-cta-section_ctaSection__o_ioD']/h2")).isDisplayed();
assertTrue(displayed);
}
@Test
void verifyPageTitle() {
String pageTitle = driver.getTitle();
assertEquals("DuckDuckGo — Privacy, simplified.", pageTitle);
}
@Test
void verifyDownloadBrowserButton() {
boolean downloadBrowserBtn = driver.findElement(By.xpath("//*[@id=\"features\"]/div[1]/section[1]/div/div[1]/div[1]/div[2]/div[1]/a/span")).isDisplayed();
assertTrue(downloadBrowserBtn);
}
@Test
void displaySearchBox() {
boolean searchBoxDisplayed = driver.findElement(By.xpath("//*[@id=\"searchbox_input\"]")).isEnabled();
assertTrue(searchBoxDisplayed);
}
@Disabled("This test is not applicable for Sprint 14")
@Test
void verifyDefaultSearchButtonText() {
String defaultSearchBtnText = driver.findElement(By.xpath("//*[@id=\"features\"]/div[1]/section[1]/div/div[1]/div[1]/div[2]/div[1]/h2")).getText();
assertEquals("Free. Fast. Private. Get our browser on all your devices.",(defaultSearchBtnText));
}
@AfterEach
public void tearDown() {
driver.close();
}
}
The output of the above test shows, the 2 tests that are annotated with @Disabled are not executed.
![](https://qaautomation.expert/wp-content/uploads/2022/05/image-81.png?w=1200)
2. Disable Test Class
When we annotate a class as @Disabled, then all the test methods present within that test class will not be executed.
In the below example, there are 2 test classes – Demo and DisabledTestsDemo. Demo class contains 1 test method whereas DisabledTestsDemo contains 5 test methods. I have annotated DisabledTestsDemo class as @Disabled which means all 5 tests present within it will not be executed.
In the below example, there is a base class that contains the initialization of webDriver as well as closing of the same.
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Base {
static WebDriver driver=null;
@BeforeEach
public void setUp() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
driver = new ChromeDriver(chromeOptions);
driver.get("https://duckduckgo.com/");
}
@AfterEach
public void tearDown() {
driver.close();
}
}
Class 1 – Demo
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class NonDisabledClass extends Base {
@Test
void verifyPageTitle() {
String pageTitle = driver.getTitle();
assertEquals("DuckDuckGo — Privacy, simplified.", pageTitle);
}
}
Class 2 – DisabledTestsDemo
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@Disabled
public class DisabledClassDemo extends Base {
@Test
void verifyBrowserText() {
boolean displayed = driver.findElement(By.xpath("//section[@class='homepage-cta-section_ctaSection__o_ioD']/h2")).isDisplayed();
assertTrue(displayed);
}
@Test
void verifyDownloadBrowserButton() {
boolean downloadBrowserBtn = driver.findElement(By.xpath("//*[@id=\"features\"]/div[1]/section[1]/div/div[1]/div[1]/div[2]/div[1]/a/span")).isDisplayed();
assertTrue(downloadBrowserBtn);
}
@Test
void displaySearchBox() {
boolean searchBoxDisplayed = driver.findElement(By.xpath("//*[@id=\"searchbox_input\"]")).isEnabled();
assertTrue(searchBoxDisplayed);
}
@Test
void verifyDefaultSearchButtonText() {
String defaultSearchBtnText = driver.findElement(By.xpath("//*[@id=\"features\"]/div[1]/section[1]/div/div[1]/div[1]/div[2]/div[1]/h2")).getText();
assertEquals("Free. Fast. Private. Get our browser on all your devices.",(defaultSearchBtnText));
}
}
The output of the above test shows, all the test methods present in the class that is annotated with @Disabled are not executed.
Below is the execution status.
![](https://qaautomation.expert/wp-content/uploads/2022/05/image-80.png?w=1109)
![](https://qaautomation.expert/wp-content/uploads/2022/05/image-5.png?w=1200)
Congratulations. We are able to understand the usage of @Disabled annotation in JUnit5. Happy Learning!!