1package test.configurationfailurepolicy;2import static org.testng.Assert.assertEquals;3import org.testng.ITestContext;4import org.testng.TestListenerAdapter;5import org.testng.TestNG;6import org.testng.annotations.BeforeClass;7import org.testng.annotations.DataProvider;8import org.testng.annotations.Test;9import org.testng.xml.XmlSuite;10import testhelper.OutputDirectoryPatch;11public class FailurePolicyTest {12 // only if this is run from an xml file that sets this on the suite13 @BeforeClass(enabled=false)14 public void setupClass( ITestContext testContext) {15 assertEquals(testContext.getSuite().getXmlSuite().getConfigFailurePolicy(), XmlSuite.CONTINUE);16 }17 @DataProvider( name="dp" )18 public Object[][] getData() {19 Object[][] data = new Object[][] {20 // params - confFail, confSkip, skipedTests21 new Object[] { new Class[] { ClassWithFailedBeforeClassMethod.class }, 1, 1, 1 },22 new Object[] { new Class[] { ClassWithFailedBeforeMethodAndMultipleTests.class }, 2, 0, 2 },23 new Object[] { new Class[] { ClassWithFailedBeforeMethodAndMultipleInvocations.class }, 4, 0, 4 },24 new Object[] { new Class[] { ExtendsClassWithFailedBeforeMethod.class }, 2, 2, 2 },25 new Object[] { new Class[] { ClassWithFailedBeforeClassMethod.class }, 1, 1, 1 },26 new Object[] { new Class[] { ExtendsClassWithFailedBeforeClassMethod.class }, 1, 2, 2 },27 new Object[] { new Class[] { ClassWithFailedBeforeClassMethod.class, ExtendsClassWithFailedBeforeClassMethod.class }, 2, 3, 3 },28 new Object[] { new Class[] { ClassWithSkippingBeforeMethod.class }, 0, 1, 1 },29 new Object[] { new Class[] { FactoryClassWithFailedBeforeMethod.class }, 2, 0, 2 },30 new Object[] { new Class[] { FactoryClassWithFailedBeforeMethodAndMultipleInvocations.class }, 8, 0, 8 },31 new Object[] { new Class[] { FactoryClassWithFailedBeforeClassMethod.class }, 2, 2, 2 },32 };33 return data;34 }35 @Test( dataProvider = "dp" )36 public void confFailureTest(Class[] classesUnderTest, int configurationFailures, int configurationSkips, int skippedTests) {37 TestListenerAdapter tla = new TestListenerAdapter();38 TestNG testng = new TestNG();39 testng.setOutputDirectory(OutputDirectoryPatch.getOutputDirectory());40 testng.setTestClasses(classesUnderTest);41 testng.addListener(tla);42 testng.setVerbose(0);43 testng.setConfigFailurePolicy(XmlSuite.CONTINUE);44 testng.run();45 verify(tla, configurationFailures, configurationSkips, skippedTests);46 }47 @Test48 public void commandLineTest_policyAsSkip() {49 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),50 "-configfailurepolicy", "skip",51 "-testclass", "test.configurationfailurepolicy.ClassWithFailedBeforeMethodAndMultipleTests" };52 TestListenerAdapter tla = new TestListenerAdapter();53 TestNG.privateMain(argv, tla);54 verify(tla, 1, 1, 2);55 }56 @Test57 public void commandLineTest_policyAsContinue() {58 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),59 "-configfailurepolicy", "continue",60 "-testclass", "test.configurationfailurepolicy.ClassWithFailedBeforeMethodAndMultipleTests" };61 TestListenerAdapter tla = new TestListenerAdapter();62 TestNG.privateMain(argv, tla);63 verify(tla, 2, 0, 2);64 }65 @Test66 public void commandLineTestWithXMLFile_policyAsSkip() {67 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),68 "-configfailurepolicy", "skip", "src/test/resources/testng-configfailure.xml" };69 TestListenerAdapter tla = new TestListenerAdapter();70 TestNG.privateMain(argv, tla);71 verify(tla, 1, 1, 2);72 }73 @Test74 public void commandLineTestWithXMLFile_policyAsContinue() {75 String[] argv = new String[] { "-log", "0", "-d", OutputDirectoryPatch.getOutputDirectory(),76 "-configfailurepolicy", "continue", "src/test/resources/testng-configfailure.xml" };77 TestListenerAdapter tla = new TestListenerAdapter();78 TestNG.privateMain(argv, tla);79 verify(tla, 2, 0, 2);80 }81 private void verify( TestListenerAdapter tla, int configurationFailures, int configurationSkips, int skippedTests ) {82 assertEquals(tla.getConfigurationFailures().size(), configurationFailures, "wrong number of configuration failures");83 assertEquals(tla.getConfigurationSkips().size(), configurationSkips, "wrong number of configuration skips");84 assertEquals(tla.getSkippedTests().size(), skippedTests, "wrong number of skipped tests");85 }86}...