Timeout exception exception

When the timeout is timeout, how can I catch the timeout? For example, in TC below, I thought that the catch block would execute after a timeout, but that is not the case. I want to do the same processing after the timeout, as if the TC ended with an exception.

@Rule 
public Timeout globalTimeout = new Timeout(3600000);

@Test   
public void tc1 throws Exception {  
try {
             //do stuff      
} 
catch (Throwable t) {           
// take screenshot, output results   
}   
}
+4
source share
3 answers

As the documentation says:

Each test runs in a new thread. If the specified timeout expires before the test completes, its execution is interrupted after Thread.interrupt(). This happens when I / O and locks are interrupted, and the methods in Object and Thread throw InterruptedException.

This means that InterruptedExceptiona timeout delay is being caused. Define the type Throwableand logging log as shown below:

@Test   
public void tc1 throws Exception {  
    try {
        //do stuff      
    } 
    catch (Throwable t) {           
        // take screenshot, output results
        if(t instanceof InterruptedException){
            // InterruptedException logging case
        }   
    }   
}
0

TimeOut. JUnit RuleChain .

@Rule
public final TestRule timeout = RuleChain
    .outerRule(new TestWatcher() {
        @Override
        protected void failed(Throwable e, Description description) {
          if (e instanceof TestTimedOutException)
            ;//do your stuff here.
        }
      })
    .around(new Timeout(3600000));
0

You can also try to test the exception as follows:

@Test(expected=TimeoutException.class)
public void tc1{  
 // call your method with parameter so that it will throws a timeoutexception
}

This means that if the method throws a TimeoutException, then the test will be fine.

-1
source

All Articles