we have used @Timeout annotation, but realized that method is not producing desired output and needs to be debug, but debug takes some time and till then @Timeout reaches, even thought asserts are valid now but still junit 5 is failing. To deal with these probleams, junit 5 supports junit.jupiter.execution.timeout.mode, it tells junit 5 when to apply time out. its values are
enabled
, disabled
, and disabled_on_debug
.
The default mode is enabled
. its values can be passed in configuration properties file src/test/resources/junit-platform.properties
package com.example.junit5.sujan;
import java.util.concurrent.TimeUnit;
public class AppleCalculator {
public int addApple(int appleBag1, int appleBag2) throws InterruptedException {
TimeUnit.SECONDS.sleep(5);
return appleBag1 + appleBag2;
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@Test
@Timeout(value = 6, unit = TimeUnit.DAYS)
void addAppleTestCase1() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.HOURS)
void addAppleTestCase2() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.MICROSECONDS)
void addAppleTestCase3() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.MILLISECONDS)
void addAppleTestCase4() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.MINUTES)
void addAppleTestCase5() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.NANOSECONDS)
void addAppleTestCase6() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.SECONDS)
void addAppleTestCase7() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
}
junit.jupiter.execution.timeout.mode=enabled
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}
timeout mode disabled, not time out will not work
package com.example.junit5.sujan;
import java.util.concurrent.TimeUnit;
public class AppleCalculator {
public int addApple(int appleBag1, int appleBag2) throws InterruptedException {
TimeUnit.SECONDS.sleep(5);
return appleBag1 + appleBag2;
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@Test
@Timeout(value = 6, unit = TimeUnit.DAYS)
void addAppleTestCase1() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.HOURS)
void addAppleTestCase2() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.MICROSECONDS)
void addAppleTestCase3() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.MILLISECONDS)
void addAppleTestCase4() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.MINUTES)
void addAppleTestCase5() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.NANOSECONDS)
void addAppleTestCase6() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.SECONDS)
void addAppleTestCase7() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
}
junit.jupiter.execution.timeout.mode=disabled
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}
if we need to use time out property but only disable it while debug only then we can use
disabled_on_debug
package com.example.junit5.sujan;
import java.util.concurrent.TimeUnit;
public class AppleCalculator {
public int addApple(int appleBag1, int appleBag2) throws InterruptedException {
TimeUnit.SECONDS.sleep(5);
return appleBag1 + appleBag2;
}
}
package com.example.junit5.sujan;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@Test
@Timeout(value = 6, unit = TimeUnit.DAYS)
void addAppleTestCase1() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.HOURS)
void addAppleTestCase2() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.MICROSECONDS)
void addAppleTestCase3() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.MILLISECONDS)
void addAppleTestCase4() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.MINUTES)
void addAppleTestCase5() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.NANOSECONDS)
void addAppleTestCase6() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
@Test
@Timeout(value = 6, unit = TimeUnit.SECONDS)
void addAppleTestCase7() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
}
junit.jupiter.execution.timeout.mode=disabled_on_debug
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}