Last active
December 16, 2015 06:58
-
-
Save sureshnath/5394852 to your computer and use it in GitHub Desktop.
Create a test for different inputs and exceptions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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