– Start
下面的例子演示了如何测试超时。
package demo05;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
class TimeoutTest {
// 测试超时方法 1
@Test
void testTimeout1() {
Integer actual = Assertions.assertTimeout(Duration.ofSeconds(2), () -> MathUtil.max(1, 2));
Integer expected = 2;
Assertions.assertEquals(expected, actual);
}
// 测试超时方法 2
@Test
@Timeout(value = 100, unit = TimeUnit.MILLISECONDS)
void testTimeout2() {
Integer actual = MathUtil.max(1, 2);
Integer expected = 2;
Assertions.assertEquals(expected, actual);
}
}
– 更多参见:JUnit 精萃
– 声 明:转载请注明出处
– Last Updated on 2019-08-12
– Written by ShangBo on 2019-08-11
– End