Home | History | Annotate | Download | only in factory
      1 package test.factory;
      2 
      3 import org.testng.Assert;
      4 import org.testng.ITestResult;
      5 import org.testng.TestListenerAdapter;
      6 import org.testng.TestNG;
      7 import org.testng.TestNGException;
      8 import org.testng.annotations.Test;
      9 
     10 import test.SimpleBaseTest;
     11 
     12 import java.util.Iterator;
     13 
     14 public class FactoryDataProviderTest extends SimpleBaseTest {
     15 
     16   @Test(description = "Test @Factory(dataProvider) on a local static data provider")
     17   public void factoryWithLocalDataProvider() {
     18     runTest(FactoryDataProviderSampleTest.class, 41, 42);
     19   }
     20 
     21   @Test(description = "Test @Factory(dataProvider) on a data provider in another class")
     22   public void factoryWithStaticDataProvider() {
     23     runTest(FactoryDataProviderStaticSampleTest.class, 43, 44);
     24   }
     25 
     26   @Test(description = "Test @Factory(dataProvider) on a non static data provider with no arg ctor")
     27   public void factoryWithNonStaticDataProvider() {
     28     runTest(FactoryDataProviderWithNoArgCtorSampleErrorTest.class, 45, 46);
     29   }
     30 
     31   @Test(expectedExceptions = TestNGException.class,
     32       description = "Should fail because the data provider is not static")
     33   public void factoryWithNonStaticDataProviderShouldFail() {
     34     runTest(FactoryDataProviderStaticSampleErrorTest.class, 43, 44);
     35   }
     36 
     37   private void runTest(Class<?> cls, int n1, int n2) {
     38     TestNG tng = create(cls);
     39     TestListenerAdapter tla = new TestListenerAdapter();
     40     tng.addListener(tla);
     41     tng.run();
     42 
     43     Assert.assertEquals(tla.getPassedTests().size(), 2);
     44     Iterator<ITestResult> iterator = tla.getPassedTests().iterator();
     45     BaseFactory t1 = (BaseFactory) iterator.next().getInstance();
     46     BaseFactory t2 = (BaseFactory) iterator.next().getInstance();
     47 //    Assert.assertTrue(t1.getN() == n1 || t1.getN() == n2);
     48 //    Assert.assertTrue(t2.getN() == n1 || t2.getN() == n2);
     49 //    System.out.println("Results:" + t1.getN() + " " + t2.getN());
     50     Assert.assertEquals(t1.getN(), n1);
     51     Assert.assertEquals(t2.getN(), n2);
     52   }
     53 }
     54