Skip to content

Instantly share code, notes, and snippets.

@sureshnath
Last active December 16, 2015 06:58
Show Gist options
  • Save sureshnath/5394852 to your computer and use it in GitHub Desktop.
Save sureshnath/5394852 to your computer and use it in GitHub Desktop.
Create a test for different inputs and exceptions
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
public class TestNGParameterisedTest {
@Test(dataProvider = "parseIntInputs")
public void testInt(String input, Object expected){
try{
Integer actual = Integer.parseInt(input);
assertEquals(actual, expected, "not equal");
}catch(Exception actualException){
assertTrue(expected instanceof Class, "wrong setup, expected exception class must be defined");
assertEquals(actualException.getClass(), expected, "wrong exception thrown");
}
}
@DataProvider(name = "parseIntInputs")
public Object[][] parseIntInputs(){
return new Object[][]{
{null, NumberFormatException.class}
,{"1", 1}
,{"-1", -1}
,{"-1.5", NumberFormatException.class}
,{"1L", NumberFormatException.class}
,{"4", 4}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment