1package test.factory;
2
3import org.testng.Assert;
4import org.testng.TestListenerAdapter;
5import org.testng.TestNG;
6import org.testng.TestNGException;
7import org.testng.annotations.Test;
8import test.SimpleBaseTest;
9
10import static org.assertj.core.api.Assertions.assertThat;
11import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
12
13public class FactoryIntegrationTest extends SimpleBaseTest {
14
15    @Test(description = "https://github.com/cbeust/testng/issues/876")
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
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
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