Updated March 28, 2023
Definition of JUnit Disable Test
Junit disable test works similarly to @ignore annotation which works in a junit 4 version. @Disabled and @Ignore annotation will work similar in junit only the difference is that we are using @Ignore annotation in junit 4 and @Disabled annotation in junit 5 version. By using the junit disabled test we can skip the execution of the test or specified group of test while applying the annotation at the test level.
Overview of JUnit Disable Test
- While applying @Disabled annotation at class level in junit all the test will be skipped. If suppose applying the annotation at method level, specified method test will be skipped.
- As per ignore annotation the disabled annotation will skips the specified test case. The parameter will optional like in a ignore annotation.
- There is only one different while skipping an test between ignore and disabled annotation is when we have applied annotation at class level, after execution of class file run count will shows 1/1 test cases is skipped.
- The count of skipped is provided in junit 5. In junit 5 the run count is shows as 3/3 test cases will skipped. We can say that out of 3, there three method will skipped.
- So we can say that as per skipped test count @Disabled will do a better job as compared to the ignore annotation. Junit disabled test is very important and useful while disabling the specified test from the code.
- If suppose we want to ignore method in specified code in junit 5 then we can use junit @Disabled annotation.
- We can also provide the reason for the specified test as per optional parameter which was provided by annotation of disabled.
- By using disabled annotation in junit it is helpful for other users who is working on same project for understanding why the specified code is disabled from the execution.
How to Use JUnit Disable Test?
- Junit disabled is used to skip the particular or group of tests for skipping the build failure. We can use junit disable test in various scenarios in our application.
- We can use junit disable test in junit version 5 it’s not possible to use in junit version 4.
- Junit disable test is used to skip the test execution from our application, sometime we not require to execute test case code because in our application is not fully developed same time we use disabled annotation to skip the test execution.
- Below steps shows to disable test in junit are as follows. We are creating the project name as JunitDisableTest. In this step we are creating the project template of junit disable test in spring boot. We are providing project group name as com.example, artifact name as JunitDisableTest, project name as JunitDisableTest and selected java version as 11. We are defining version of spring boot as 2.7.0
Group – com.example Artifact name – JunitDisableTest
Name – JunitDisableTest Spring boot – 2.7.0
Project – Maven Java – 11
Package name – com.example. JunitDisableTest
Project Description – Project for JunitDisableTest
- In this step we are extracting the downloaded project and opening the same by using spring tool suite are as follows.
- In this step we are checking all the project structures and its files are as follows. Also, we are checking the pom.xml file is created or not. If suppose this file is not created then we need to create the same manually. In below example this file is created, so we have no need to create it manually.
- In this step we are adding the junit dependency in junit disable test We are adding junit dependency.
Code:
<dependencies>
<dependency>
<groupId> org.junit.jupiter </groupId>
<artifactId> junit-jupiter-engine </artifactId>
<version> 5.3.1 </version>
<scope> junit- disable-test </scope>
</dependency>
<dependency>
<groupId> junit </groupId>
<artifactId> junit </artifactId>
<version> 4.12 </version>
</dependency>
</dependencies>
<reporting>
<plugins>
<plugin>
<groupId> org.apache.maven.plugins </groupId>
<artifactId> maven-surefire-report-plugin </artifactId>
<version> 2.19.1 </version>
</plugin>
</plugins>
</reporting>
- After adding the dependency we are creating the junit disable test on class are as follows. We are creating the class name as JunitDisableTest are as follows. We can see that we have declared the disabled annotation before the class so it will skip all the test from JunitDisableTest class. Also in below example we have used two methods junit1 and junit2. We have defined those methods within class so those method also skipped from the test suite.
Code:
@Disabled ("Junit Disabled Test")
public class JunitDisableTest {
@Test
void junit1 () {
assertEquals(3, 1 + 5);
}
@Test
void junit2() {
assertEquals(5, 2 + 6);
}
}
Junit Disable Test Method
- We can disable test method by specifying the disabled annotation with in method. If suppose we have used disabled annotation before the class it will disabled the whole class. But is suppose we have used disabled annotation within class and into method so it will apply for specified method to disable it from test execution.
- Below example shows disable test method are as follows. We are creating the class name as JunitDisableTest1 are as follows. We can see that we have declared the disabled annotation after the class so it will skip the methods from JunitDisableTest1 class. We have used two methods junit1 and junit2. We have defined those methods within class and used @Disabled annotation on each method so it will skip test execution.
Code:
public class JunitDisableTest1 {
@Disabled ("Junit disabled test method")
@Test
void Junit1 () {
assertEquals (7, 5 + 5);
}
@Disabled ("Junit disabled test method")
@Test
void Junit2 () {
assertEquals (5, 2 + 2);
}
}
Examples
Below is the example of junit disable class test are as follows.
Code:
@Disabled ("Junit Disabled Test")
public class DisabledTest {
@Test
void test () {
assertEquals (5, 1 + 9);
}
}
Below is the example of junit disable method test are as follows.
Code –
public class DisabledTest {
@Disabled ("Junit Disabled Test")
@Test
void test () {
assertEquals (9, 1 + 7);
}
}
Conclusion
We can ignore all test class from specified application by using junit disabled annotation. We can write junit disable annotation as @Disabled. By using disabled annotation in junit it is helpful for other users who is working on same project for understanding why the specified code is disabled from the execution.
Recommended Article
This is a guide to JUnit Disable Test. Here we discuss the Definition, overview, How to use JUnit Disable Test, examples with code implementation respectively. You may also have a look at the following articles to learn more –