I am using testNG, Rest-Assured and Java for API automation. Is there any way to pause the execution for a specific time if any event/exception occurred?

  • 1
    The same question is basically on StackOverflow: stackoverflow.com/questions/15938538/… – FDM Dec 5 '18 at 7:56
  • "if any event exception occurred" the question here is different. – João Farias Dec 5 '18 at 10:53
  • My answer to this would be influenced by what problem you're trying to solve using exception handling. What are you trying to do? – anonygoose Dec 5 '18 at 15:01
  • @JoãoFarias how is that different? You just put an IF around it... – FDM Dec 6 '18 at 7:27
  • I quoted the difference in context: The Stackoverflows want to simply stop - this question wants to stop if a specific event happens. Similar, but different situations. – João Farias Dec 6 '18 at 13:11

FDM is right. You can use the approach that is suggested in corresponding post on SO. So you can just use Thread.sleep();

Regarding sleeping on exception you can put your Thread.sleep() inside catch clause like:

    // my test code
}catch(Exception e){
    try {
    } catch (InterruptedException e1) {
    throw e;

Not to break the framework logic you need to throw the exception to the next handling layers so that TesnNG knows the test has failed.

| improve this answer | |

The cleaner (therefore least error-prone) way of doing it is by using aspect-oriented programming - AspectJ for Java.

You would need to define an aspect with the following pointcut that catches after an specific exception is thrown in any method with the annotation @Test

after() throwing(ExceptionYouWantToCatch e): execution(* (@Test *).*(..).main(*)) {

This way, all tests are automatically fetched to this behavior - and you can change it but only updating this aspect.

See this Stack Overflow question for catching exceptions.

And this for catching Annotated methods.

| improve this answer | |
  • I am not an expert in AspectJ so that I wonder if this approach would pause exactly that thread that is executing a test? – Alexey R. Dec 5 '18 at 13:57
  • The aspect behavior is injected on the method - it's similar to Traits in PHP, the context is carried out together. – João Farias Dec 6 '18 at 13:08
  • Thank you. Would give this a try definitely. – Prabhudatta Choudhury Dec 21 '18 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.