Loading
 
What's Next: View All Tutorial →

How to set Timeout 1s for the @Test Method in Junit

A simple program to set Timeout 1s for the @Test Method in Junit   @Test(timeout = 1000) public void testInfiniteLoop() { int i = 1; for (;;) { System.out.println(i++); } } output: ...

Published Date: 20/02/2015  Last Modified Date: 20/02/2015    New Post




A simple program to set Timeout 1s for the @Test Method in Junit
 
@Test(timeout = 1000)
    public void testInfiniteLoop() {
        int i = 1;
        for (;;) {
            System.out.println(i++);
        }
    }

output:

test timed out after 1000 milliseconds
org.junit.runners.model.TestTimedOutException
	at java.io.FileOutputStream.writeBytes(Native Method)
	at java.io.FileOutputStream.write(FileOutputStream.java:307)
	at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:82)
	at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:140)
	at java.io.PrintStream.write(PrintStream.java:482)
	at org.apache.maven.surefire.booter.ForkingRunListener.writeTestOutput(ForkingRunListener.java:178)
	at org.apache.maven.surefire.report.ConsoleOutputCapture$ForwardingPrintStream.write(ConsoleOutputCapture.java:56)
	at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
	at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
	at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
	at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
	at java.io.PrintStream.write(PrintStream.java:527)
	at java.io.PrintStream.print(PrintStream.java:597)
	at java.io.PrintStream.println(PrintStream.java:736)
	at com.mycompany.app.UtilTest.testInfiniteLoop(UtilTest.java:101)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:483)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.lang.Thread.run(Thread.java:745)




We can define timeout value for all the test method in the class using @Rule method
 
public class UtilTest {

@Rule
    public Timeout globalTimeout = new Timeout(5000); // 5 seconds max per


@Test
    public void testInfiniteLoop() {
        int i = 1;
        for (;;) {
            System.out.println(i++);
        }
    }


}


 
Awaiting for Administrator approval





Tags: JUnit

← Back to list


Related Post




×