sbabcoc / TestNG-Foundation

TestNG Foundation is a lightweight collection of TestNG listeners, interfaces, and static utility classes that supplement and augment the functionality provided by the TestNG API.
Apache License 2.0
13 stars 5 forks source link

NPE when Factory is defined as test class constructor #8

Closed yaroslav-orel closed 6 years ago

yaroslav-orel commented 6 years ago

GIven test class

public class ConstructorFactory {

    public ConstructorFactory(){}

    @DataProvider
    public Object[] ints(){return new Object[]{1, 2, 3};}

    @Factory(dataProvider = "ints")
    public ConstructorFactory(int i){ }

    @Test
    public void test(){}
}

Running this test class with testng with ListenerChain applied will cause NPE

        ListenerChain lc = new ListenerChain();
        TestListenerAdapter tla = new TestListenerAdapter();

        TestNG testNG = new TestNG();
        testNG.setTestClasses(new Class[]{ConstructorFactory.class});
        testNG.addListener((ITestNGListener) lc);
        testNG.addListener((ITestNGListener) tla);
        testNG.run();

This is because Method method reference which is passed to transform() is null and in this case IFactoryAnnotation annotation reference is not null.

Partial stacktrace:

java.lang.NullPointerException
    at com.nordstrom.automation.testng.ListenerChain.attachListeners(ListenerChain.java:593)
    at com.nordstrom.automation.testng.ListenerChain.transform(ListenerChain.java:146)
    at org.testng.internal.annotations.JDK15AnnotationFinder.transform(JDK15AnnotationFinder.java:180)
    at org.testng.internal.annotations.JDK15AnnotationFinder.findAnnotation(JDK15AnnotationFinder.java:231)
    at org.testng.internal.annotations.JDK15AnnotationFinder.findAnnotation(JDK15AnnotationFinder.java:216)
    at org.testng.internal.ClassHelper.findDeclaredFactoryMethods(ClassHelper.java:146)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:133)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:40)
    at org.testng.TestRunner.initMethods(TestRunner.java:403)
    at org.testng.TestRunner.init(TestRunner.java:252)
    at org.testng.TestRunner.init(TestRunner.java:222)
    at org.testng.TestRunner.<init>(TestRunner.java:163)
sbabcoc commented 6 years ago

Thanks for the issue report and the PR of the fix! There are a few stylistic differences between your changes and the existing code that I'd like to resolve before I merge this (wildcard imports, conditionals without braces). Would you object to me taking care of these items, or would you prefer to apply these yourself?

yaroslav-orel commented 6 years ago

@sbabcoc not at all. Go ahead and make all the necessary changes

sbabcoc commented 6 years ago

@yaroslav-orel: I used your revisions as the starting point for a more comprehensive fix. Thanks for the submission!

sbabcoc commented 6 years ago

@yaroslav-orel: Release 1.6.1 should be available soon in a Maven archive near you.