From 3103d55f6e2a054a95428358de715c9ea40023cd Mon Sep 17 00:00:00 2001 From: bj-9527 Date: Tue, 22 Mar 2022 14:07:48 +0800 Subject: [PATCH] SuiteRunner could not be initial by default Configuration Fixes #2743 --- CHANGES.txt | 1 + .../src/main/java/org/testng/SuiteRunner.java | 4 ++ .../test/configuration/ConfigurationTest.java | 10 +++ .../issue2743/SuiteRunnerIssueTestSample.java | 71 +++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 testng-core/src/test/java/test/configuration/issue2743/SuiteRunnerIssueTestSample.java diff --git a/CHANGES.txt b/CHANGES.txt index 46be0f07ba..8ffd633ff8 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,6 @@ Current 7.6.0 +Fixed: GITHUB-2743: SuiteRunner could not be initial by default Configuration (Nan Liang) Fixed: GITHUB-2729: beforeConfiguration() listener method should be invoked for skipped configurations as well(Nan Liang) Fixed: assertEqualsNoOrder for Collection and Iterators size check was missing (Adam Kaczmarek) Fixed: GITHUB-2709: Testnames not working together with suites in suite (Martin Aldrin) diff --git a/testng-core/src/main/java/org/testng/SuiteRunner.java b/testng-core/src/main/java/org/testng/SuiteRunner.java index ea43e07542..80e9e1d60a 100644 --- a/testng-core/src/main/java/org/testng/SuiteRunner.java +++ b/testng-core/src/main/java/org/testng/SuiteRunner.java @@ -18,6 +18,7 @@ import org.testng.internal.invokers.IInvocationStatus; import org.testng.internal.invokers.IInvoker; import org.testng.internal.invokers.InvokedMethod; +import org.testng.internal.objects.ObjectFactoryImpl; import org.testng.internal.thread.ThreadUtil; import org.testng.reporters.JUnitXMLReporter; import org.testng.reporters.TestHTMLReporter; @@ -148,6 +149,9 @@ private void init( List localMethodInterceptors = Optional.ofNullable(methodInterceptors).orElse(Lists.newArrayList()); setOutputDir(outputDir); + if (configuration.getObjectFactory() == null) { + configuration.setObjectFactory(new ObjectFactoryImpl()); + } if (suite.getObjectFactoryClass() == null) { objectFactory = configuration.getObjectFactory(); } else { diff --git a/testng-core/src/test/java/test/configuration/ConfigurationTest.java b/testng-core/src/test/java/test/configuration/ConfigurationTest.java index 8792f3516c..44ccdb3213 100644 --- a/testng-core/src/test/java/test/configuration/ConfigurationTest.java +++ b/testng-core/src/test/java/test/configuration/ConfigurationTest.java @@ -2,7 +2,9 @@ import java.util.Arrays; import org.testng.Assert; +import org.testng.TestNG; import org.testng.annotations.Test; +import test.configuration.issue2743.SuiteRunnerIssueTestSample; import test.configuration.sample.ConfigurationTestSample; import test.configuration.sample.ExternalConfigurationClassSample; import test.configuration.sample.MethodCallOrderTestSample; @@ -29,4 +31,12 @@ public void testSuite() { testConfiguration(SuiteTestSample.class); Assert.assertEquals(Arrays.asList(1, 2, 3, 4, 5), SuiteTestSample.m_order); } + + @Test + public void testSuiteRunnerWithDefaultConfiguration() { + TestNG testNG = create(SuiteRunnerIssueTestSample.class); + testNG.run(); + + Assert.assertEquals(testNG.getStatus(), 0); + } } diff --git a/testng-core/src/test/java/test/configuration/issue2743/SuiteRunnerIssueTestSample.java b/testng-core/src/test/java/test/configuration/issue2743/SuiteRunnerIssueTestSample.java new file mode 100644 index 0000000000..bd9b9fdcac --- /dev/null +++ b/testng-core/src/test/java/test/configuration/issue2743/SuiteRunnerIssueTestSample.java @@ -0,0 +1,71 @@ +package test.configuration.issue2743; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import org.testng.IClassListener; +import org.testng.IInvokedMethodListener; +import org.testng.ISuite; +import org.testng.ITestNGMethod; +import org.testng.ITestRunnerFactory; +import org.testng.SuiteRunner; +import org.testng.TestRunner; +import org.testng.annotations.Test; +import org.testng.internal.Configuration; +import org.testng.internal.IConfiguration; +import org.testng.reporters.JUnitXMLReporter; +import org.testng.reporters.TestHTMLReporter; +import org.testng.xml.XmlClass; +import org.testng.xml.XmlSuite; +import org.testng.xml.XmlTest; + +public class SuiteRunnerIssueTestSample { + + @Test + public void suiteRunnerSample() { + XmlSuite suite = new XmlSuite(); + suite.setName("TestSuite"); + XmlTest test = new XmlTest(suite); + test.setName("ChildTests"); + List classes = new ArrayList<>(); + classes.add(new XmlClass("test.configuration.suites.IssueTest")); + test.setXmlClasses(classes); + + final IConfiguration configuration = new Configuration(); + final boolean useDefaultListeners = true; + + SuiteRunner suiteRunner = + new SuiteRunner( + configuration, + suite, + "outputDir", + new ITestRunnerFactory() { + + @Override + public TestRunner newTestRunner( + ISuite suite, + XmlTest xmlTest, + Collection listeners, + List classListeners) { + TestRunner runner = + new TestRunner( + configuration, + suite, + xmlTest, + false /* skipFailedInvocationCounts */, + listeners, + classListeners); + if (useDefaultListeners) { + runner.addListener(new TestHTMLReporter()); + runner.addListener(new JUnitXMLReporter()); + } + + return runner; + } + }, + useDefaultListeners, + Comparator.comparingInt(ITestNGMethod::getPriority)); + suiteRunner.run(); + } +}