testNG: How to write time test

 

Some time performance is critical. Lets a repository call does return the response withing request time out units then it will cause the application to break. Just to keep those things and performance in mind, testNG tests can be calibrate on the measure of time in milli seconds.

The “Time Test” means if an unit test takes longer than the specified number of milliseconds to run, the test will terminated and mark as failed.

import org.testng.annotations.*;public class TestNGTest4 {

	@Test(timeOut = 1000)  
	public void infinity() {  
		while (true); 
              // userRepository.getAllUsersInEntireWorld(); 
	}  

}

In above example, the infinity() method will not return, so the TestNG engine will mark it as failed and throw an exception

FAILED: infinity
org.testng.internal.thread.ThreadTimeoutException: 
Method public void TestNGTest4.infinity() didn't finish within the time-out 1000
... Removed 18 stack frames

 

testNG : How to ignore or disable a test in testNG

Some times a failing test causes the build to fail. In that cases before the test is fixed a quick remedy is to disable the test to get the build passed. That can be done easily through annotation.

@Test(enabled=false)

The TestNG engine will just bypass this method.

import org.testng.annotations.*;

public class TestNGTest3 {

	@Test(enabled=false)
	public void divisionWithException() {  
	  System.out.println("Method is not ready yet");
	}  

}

In above example, TestNG will not test the divisionWithException() method.