A logo showing the text blog.marcnuri.com
Español
Home»Java»JUnit 5 - How to disable or ignore tests

Recent Posts

  • Windows 11: How to enable the built-in Administrator account
  • Bash: How to wait until a file exists
  • Gatsby 5: How to query data from multiple GraphQL sources
  • Bash: How to iterate through a list of strings
  • Kubernetes Client for Java: How to set up the underlying HTTP client

Categories

  • Front-end
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • December 2015
  • November 2015
  • November 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

JUnit 5 - How to disable or ignore tests

2023-08-25 in Java tagged Java / Quickie / Testing / JUnit / JUnit 5 by Marc Nuri | Last updated: 2023-08-25
Versión en Español

In JUnit 4, you could apply the @Ignore annotation to a test method to skip its execution. This annotation is no longer available in JUnit 5, you should use the @Disabled annotation instead.

The JUnit 5 documentation covers test disabling extensively, the following sections showcase the usage of the @Disabled annotation.

@Disabled annotation applied to a class

You can apply the @Disabled annotation to a class to disable all the tests in that class (suite).

DisabledClassTest.java
@Disabled
class DisabledClassTest {
    @Test
    void test() {
        fail("This test should not be executed");
    }
}

@Disabled annotation applied to a method

You can apply the @Disabled annotation to a method to disable that specific test.

DisabledMethodTest.java
class DisabledMethodTest {
    @Disabled
    @Test
    void test() {
        fail("This test should not be executed");
    }
}

In addition to the test being disabled, none of the method-lifecycle callbacks such as @BeforeEach or @AfterEach will be invoked for that test.

Providing a reason for disabling a test

One of my favorite features of the @Disabled annotation as compared to JUnit 4 @Ignore annotation is the ability to provide a reason for disabling a test.

DisabledClassTest.java
@Disabled("This test is disabled because of a bug in the system BUG-1337")
class DisabledClassTest {
    @Test
    void test() {
        fail("This test should not be executed");
    }
}
Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation

← JUnit 5 - How to run unit tests in MavenHow to convert File to Path and Path to File in Java →
© 2007 - 2023 Marc Nuri