forked from testng-team/testng
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support ITestNGFactory customisation
Closes testng-team#3059
- Loading branch information
1 parent
4af6e94
commit 38b4fd4
Showing
13 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
testng-core/src/test/java/test/listeners/factory/ExampleListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package test.listeners.factory; | ||
|
||
import org.testng.IExecutionListener; | ||
|
||
public class ExampleListener implements IExecutionListener { | ||
|
||
public static ExampleListener instance; | ||
|
||
public ExampleListener() { | ||
setInstance(this); | ||
} | ||
|
||
private static synchronized void setInstance(ExampleListener instance) { | ||
if (ExampleListener.instance == null) { | ||
ExampleListener.instance = instance; | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
testng-core/src/test/java/test/listeners/factory/SampleTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package test.listeners.factory; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
public class SampleTestCase { | ||
@Test | ||
public void testMethod() {} | ||
} |
30 changes: 30 additions & 0 deletions
30
testng-core/src/test/java/test/listeners/factory/SampleTestFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package test.listeners.factory; | ||
|
||
import org.testng.ITestNGListener; | ||
import org.testng.ITestNGListenerFactory; | ||
import org.testng.internal.objects.InstanceCreator; | ||
|
||
public class SampleTestFactory implements ITestNGListenerFactory { | ||
|
||
public static ITestNGListenerFactory instance; | ||
|
||
private boolean invoked = false; | ||
|
||
public boolean isInvoked() { | ||
return invoked; | ||
} | ||
|
||
public SampleTestFactory() { | ||
setInstance(this); | ||
} | ||
|
||
private static void setInstance(ITestNGListenerFactory instance) { | ||
SampleTestFactory.instance = instance; | ||
} | ||
|
||
@Override | ||
public ITestNGListener createListener(Class<? extends ITestNGListener> listenerClass) { | ||
invoked = true; | ||
return InstanceCreator.newInstance(listenerClass); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
testng-core/src/test/java/test/listeners/factory/TestNGFactoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package test.listeners.factory; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import java.util.List; | ||
import org.testng.CommandLineArgs; | ||
import org.testng.TestNG; | ||
import org.testng.annotations.Test; | ||
import test.SimpleBaseTest; | ||
|
||
public class TestNGFactoryTest extends SimpleBaseTest { | ||
|
||
@Test(description = "GITHUB-3059") | ||
public void testListenerFactoryViaConfigurationArg() { | ||
String[] args = | ||
new String[] { | ||
CommandLineArgs.LISTENER_FACTORY, | ||
SampleTestFactory.class.getName(), | ||
CommandLineArgs.TEST_CLASS, | ||
SampleTestCase.class.getName(), | ||
CommandLineArgs.LISTENER, | ||
ExampleListener.class.getName() | ||
}; | ||
TestNG testng = TestNG.privateMain(args, null); | ||
assertThat(SampleTestFactory.instance).isNotNull(); | ||
assertThat(ExampleListener.instance).isNotNull(); | ||
assertThat(testng.getStatus()).isZero(); | ||
} | ||
|
||
@Test(description = "GITHUB-3059") | ||
public void testListenerFactoryViaTestNGApi() { | ||
TestNG testng = new TestNG(); | ||
SampleTestFactory factory = new SampleTestFactory(); | ||
testng.setListenerFactory(factory); | ||
testng.setListenerClasses(List.of(ExampleListener.class)); | ||
testng.setTestClasses(new Class[] {SampleTestCase.class}); | ||
testng.run(); | ||
assertThat(testng.getStatus()).isZero(); | ||
assertThat(factory.isInvoked()).isTrue(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters