Some times our method is not behaving as required or some issue occurred and we need to disable some test cases for some. one way is to comment on the entire test case, but it's not the ideal way and best practice, for this JUnit 5 provides us with an @Disabled
annotation.
JUnit 4
@Ignored
= JUnit 5@Disabled
assume we have a working program
package com.example.junit5.sujan;
public class AppleCalculator {
public int addApple(int appleBag1, int appleBag2) {
return appleBag1 + appleBag2;
}
public int subApple(int apple1, int apple2) {
return appleBag1 * appleBag2; // wrong output
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@Test
void addAppleTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
void subAppleTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}
but we realized that subApple(int apple1, int apple2) is returning wrong output and failing our build, hence we need to disable it.
package com.example.junit5.sujan;
public class AppleCalculator {
public int addApple(int apple1, int apple2) {
return apple1 + apple2;
}
public int subApple(int apple1, int apple2) {
return apple1 * apple2;
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@Test
void addAppleTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Disabled
void subAppleTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}
if required we can disable the entire test class well.
package com.example.junit5.sujan;
public class AppleCalculator {
public int addApple(int apple1, int apple2) {
return apple1 + apple2;
}
public int subApple(int apple1, int apple2) {
return apple1 * apple2;
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Disabled
class AppleCalculatorTest {
@Test
void addAppleTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
void subAppleTest() {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(1, appleCalculator.subApple(2, 1), "2 apple - 1 apple is 1 apple");
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}
Run Disabled Test
We disable test case using @Disabled
annotation if it's not behaving properly or dependent on some external factors, But once in a while we might wanna run and test all disabled test case just to check if they still behaving differently, 1 way is to remove all @Disabled
annotation from test case class and then put back after execution or a better way is to explicitly tell JUnit to run all @Disabled
test cases.
create a file name junit-platform.properties
in src/test/resources
.
junit.jupiter.conditions.deactivate=org.junit.*DisabledCondition
package com.example.junit5.sujan;
public class AppleCalculator {
public int addApple(int apple1, int apple2) {
return apple1 + apple2;
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@Test
@Disabled
void addAppleTest() {
System.out.println("AppleCalculatorTest.addAppleTest");
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
}