JUnit 5 is a widely used testing framework in the Java ecosystem. It is the successor of JUnit 4 and is designed to address its limitations. JUnit framework allows the developers to write and run the tests for their Java code. allows to use of the following features.
- It ensures that the code functions correctly and continues to work as expected when changes are made.
- JUnit 5 provides a variety of annotations and one such annotation is @Disabled.
In this article, let us understand about @Disabled annotation in JUnit 5.
@Disabled in JUnit 5
@Disabled annotation in JUnit 5 is used to mark a method that should be disabled or ignored during the test execution. When this @Disabled annotation is applied at the class level, all the underlying test methods are disabled. It is similar to @Ignore annotation in JUnit 4.
Examples of @Disabled
In general, the @Disabled annotation has both method-level and class-level use cases. Let us understand the both method-level and class-level usages of @Disabled annotation.
1. Method-level
In the below code, we can find the use of @Disabled annotation on a method.
2. Class-level
In the below code, we can find the use of @Disabled annotation on a class containing many test cases.
Step By Step Implementation
Step 1: Creation of Project
- Go to the file menu click new and navigate to Spring Starters project. If you don't find the Spring Starters project immediately after the new one, then click other and find the Spring Starters project.
👁 Options to BootStrap of Spring Boot Starter Project
- File > new > Spring Starters project.
👁 BootStrap of Spring Boot Starter Project
- Name your project and configure the default options given if necessary.
👁 Build Tools selection window
- Necessary Configurations:
- Type: Maven
- Packaging: Jar
- Java Version: 8
- Language: Java
- Make sure that, you have chosen the type as 'Maven' ( You can also choose `Gradle`, but for the demonstration, I am using Maven build management tool) and the version of Java should be at least 8.
- Add dependencies If you need any, otherwise, click `finish`.
👁 Dependency selection Window
Step 2: Adding dependencies
Let us configure our pom.xml file with the following dependencies:
JUnit Jupiter API:
The JUnit Jupiter API is the part of JUnit 5 framework which provides annotations, assertions, and other features for defining and running test cases and serves as a programming model for writing tests in Java.
To add the JUnit Jupiter API in your Spring Boot Application, use the following dependency in your pom.xml file under dependencies.
Project Setup
Now, that the project setup is ready, let us look into an example to understand how to write and run test cases of JUnit using the Maven tool.
- Step 1: Create a new package in the , right-click on > new > package.
👁 Package creation for project
- Extra available configuration for Package is below
👁 package configuration
- Step 2: Create a class in the package and name it, for this illustration we will name it Addition.java to create a class, right-click on package > new > class.
👁 instruction for creation of a new class
- New Java Class configuration options is below
👁 configuration for a new class
- Step 3: write the logic which you want to test in Addition.java.
- Step 4: Now, create a test case inside the package by right click on the package > new > others > java > JUnit > JUnit test case.
👁 Test case creation instruction
- New JUnit Test case configuration is below
👁 configuration for a new test case
Example 1:
Test Script: Let us write the first test case i.e. Testcase1, inside it we have two methods named test1, and test2.
Explanation of the above program:
In the above example, we have provided method-level implementation for @Disabled annotation. To do this, we have defined two test methods annotated with @Test, those are test1 and test2 descriptions, and details for them are below.
- In both methods, we have provided the Same implementation,
- but for the test1 method, we have added the @Disabled annotation.
- In the implementation part, we are creating the addition object for the Addition class
- It will store the calculated sum in a variable called actual and the expected sum in a variable called expected
- we have also used assertEquals to verify whether the actual and expected values are equal or not.
Steps to run the application:
To run the application or project, navigate to your project in the project explorer, right-click on the project > Run as > Spring Boot App.
👁 ts18
Output:
- JUnit view: The below picture depicts Testcase 1 executed in 0.011s
👁 Test case status
- Console view: The image depicts the log of test case execution.
👁 testcase status on console
Example 2:
Test Script: Let us write the first test case i.e. Testcase2.
Explanation:
In the above test case, we have provided class-level implementation for @Disabled annotation.
- In the Testcase2 class, we have the method called test annotated with @Test
- the class contains creating the addition object for the Addition class
- The method will store the calculated sum in a variable called actual and the expected sum in a variable called expected
- we have also used assertEquals to verify whether the actual and expected values are equal or not.
Output:
👁 Test case status
- Console view: Nothing is displayed on the console because the test class is disabled
Conclusion
Overall we have covered @Disabled annotation in detail, also we different use cases of @Disabled annotation in JUnit 5 Testing on
- Testing Class - will skip the complete class from execution
- Testing Methods - will skip only the annotated methods.