we might need to create a test case which measures the performace of the method, that method should respond withing this much amount of time, if not then fail the test. to achieve this functionality , junit5 provides us @Timeout annotation.
package com.example.junit5.sujan;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AppleCalculatorTest {
@Test
@Timeout(4)
void addAppleTest() throws InterruptedException {
AppleCalculator appleCalculator = new AppleCalculator();
assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
}
}
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;
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}
parameter provided in @Timeout annotation as parameter is by default in seconds, but its configurable. we can change it accourdingly. default value is TimeUnit.SECONDS
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");
}
}
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}