timeout test in testng

Sometimes we want to fail the test if it takes more than specific time to execute. TestNG allows you to specify the timeout using timeOut parameter in Test annotation as shown in below example.
 
package org.softpost;

import org.testng.annotations.Test;

/**
 * Created by Sagar on 25-06-2016.
 */
public class TimeoutTests {

    @Test(timeOut = 5000)
    public void testServerResponse() throws Exception{
        Thread.sleep(6000);
        System.out.println("Test Executed");
    }
}

Here is the output of above code.
 
org.testng.internal.thread.ThreadTimeoutException:Method 
org.testng.internal.TestNGMethod.testServerResponse() didn’t finish within the time-out 5000

Web development and Automation testing

solutions delivered!!