📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
In this article, we will discuss the usage of JUnit @Disabled annotation with an example.
JUnit Disabling Tests Example
JUnit @Disabled annotation is the easiest way to disable a test. It can be applied to a test method as well as to the Class itself. If @Disabled annotation is applied to the class, then all the tests present in the class gets disabled. We can also specify a reason message for documentation purposes.Disable Test Class
@Disabled
annotation to disable the entire test class. If @Disabled annotation is applied to the class, then all the tests present in the class get disabled. import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @Disabled class DisabledClassDemo { @Test void test1() { System.out.println("test 1"); } @Test void test2() { System.out.println("test 2"); } @Test void test3() { System.out.println("test 2"); } }
Disable Test Method
Use @Disabled annotation to disable the JUnit test method.import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
class DisabledTestsDemo {
@Disabled
@Test
void testWillBeSkipped() {
}
@Test
void testWillBeExecuted() {
}
}
Conclusion
In this post, we used @Disabled annotation to disable or ignore the tests in JUnit 5. We can disable either class or test method.JUnit 5 Related Posts
- Overview of JUnit 5
- JUnit 5 Maven Example
- JUnit 5 Standard Test Class Basic Template
- JUnit 5 Annotations with Examples
- JUnit 5 Assertions with Examples
- JUnit 5 Nested Tests Example
- JUnit 5 Disabling Tests Examples
- JUnit 5 Display Names Example
- JUnit 5 Repeated Tests with Examples
- JUnit 5 Exception Testing with Example
Comments
Post a Comment
Leave Comment