1 package test.factory; 2 3 import org.testng.Assert; 4 import org.testng.TestListenerAdapter; 5 import org.testng.TestNG; 6 import org.testng.TestNGException; 7 import org.testng.annotations.Test; 8 import test.SimpleBaseTest; 9 10 import static org.assertj.core.api.Assertions.assertThat; 11 import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown; 12 13 public class FactoryIntegrationTest extends SimpleBaseTest { 14 15 @Test(description = "https://github.com/cbeust/testng/issues/876") testExceptionWithNonStaticFactoryMethod()16 public void testExceptionWithNonStaticFactoryMethod() { 17 TestNG tng = create(GitHub876Sample.class); 18 try { 19 tng.run(); 20 failBecauseExceptionWasNotThrown(TestNGException.class); 21 } catch (TestNGException e) { 22 assertThat(e).hasMessage("\nCan't invoke public java.lang.Object[] test.factory.GitHub876Sample.createInstances(): either make it static or add a no-args constructor to your class"); 23 } 24 } 25 26 @Test testNonPublicFactoryMethodShouldWork()27 public void testNonPublicFactoryMethodShouldWork() { 28 TestNG tng = create(NonPublicFactoryMethodSample.class); 29 TestListenerAdapter tla = new TestListenerAdapter(); 30 tng.addListener(tla); 31 32 tng.run(); 33 34 Assert.assertEquals(tla.getPassedTests().size(), 2); 35 } 36 37 @Test testExceptionWithBadFactoryMethodReturnType()38 public void testExceptionWithBadFactoryMethodReturnType() { 39 TestNG tng = create(BadFactoryMethodReturnTypeSample.class); 40 try { 41 tng.run(); 42 failBecauseExceptionWasNotThrown(TestNGException.class); 43 } catch (TestNGException e) { 44 assertThat(e).hasMessage("\ntest.factory.BadFactoryMethodReturnTypeSample.createInstances MUST return [ java.lang.Object[] or org.testng.IInstanceInfo[] ] but returns java.lang.Object"); 45 } 46 } 47 } 48