-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow ITestObjectFactory injection via listeners #2677
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions
12
testng-core/src/main/java/org/testng/internal/objects/DefaultTestObjectFactory.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,12 @@ | ||
package org.testng.internal.objects; | ||
|
||
import org.testng.ITestObjectFactory; | ||
|
||
/** | ||
* Intended to be the default way of instantiating objects within TestNG. Intentionally does not | ||
* provide any specific implementation because the interface already defines the default behavior. | ||
* This class still exists to ensure that we dont use an anonymous object instantiation so that its | ||
* easy to find out what type of object factory is being injected into our object dispensing | ||
* mechanisms. | ||
*/ | ||
public class DefaultTestObjectFactory implements ITestObjectFactory {} |
78 changes: 78 additions & 0 deletions
78
testng-core/src/test/java/test/objectfactory/ObjectFactoryTest.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,78 @@ | ||
package test.objectfactory; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.testng.TestNG; | ||
import org.testng.TestNGException; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
import org.testng.xml.XmlSuite; | ||
import test.SimpleBaseTest; | ||
import test.objectfactory.github1131.EmptyConstructorSample; | ||
import test.objectfactory.github1131.IntConstructorSample; | ||
import test.objectfactory.github1131.MyObjectFactory; | ||
import test.objectfactory.github1131.StringConstructorSample; | ||
import test.objectfactory.github1827.GitHub1827Sample; | ||
import test.objectfactory.issue2676.LocalSuiteAlteringListener; | ||
import test.objectfactory.issue2676.LoggingObjectFactorySample; | ||
import test.objectfactory.issue2676.TestClassSample; | ||
|
||
public class ObjectFactoryTest extends SimpleBaseTest { | ||
|
||
@Test(description = "GITHUB-2676") | ||
public void ensureObjectFactoryIsInvokedWhenAddedViaListeners() { | ||
TestNG testng = create(TestClassSample.class); | ||
testng.addListener(new LocalSuiteAlteringListener()); | ||
testng.run(); | ||
assertThat(LoggingObjectFactorySample.wasInvoked).isTrue(); | ||
} | ||
|
||
@Test( | ||
expectedExceptions = TestNGException.class, | ||
expectedExceptionsMessageRegExp = ".*Check to make sure it can be instantiated", | ||
description = "GITHUB-1827") | ||
public void ensureExceptionThrownWhenNoSuitableConstructorFound() { | ||
|
||
TestNG testng = create(GitHub1827Sample.class); | ||
testng.run(); | ||
} | ||
|
||
@Test(dataProvider = "dp", description = "GITHUB-1131") | ||
public void factoryWithEmptyConstructorShouldWork(boolean bool) { | ||
testFactory(bool, EmptyConstructorSample.class); | ||
assertThat(MyObjectFactory.allParams).containsExactly(new Object[] {}, new Object[] {}); | ||
} | ||
|
||
@Test(dataProvider = "dp", description = "GITHUB-1131") | ||
public void factoryWithIntConstructorShouldWork(boolean bool) { | ||
testFactory(bool, IntConstructorSample.class); | ||
assertThat(MyObjectFactory.allParams).containsExactly(new Object[] {1}, new Object[] {2}); | ||
} | ||
|
||
@Test(dataProvider = "dp", description = "GITHUB-1131") | ||
public void factoryWithStringConstructorShouldWork(boolean bool) { | ||
testFactory(bool, StringConstructorSample.class); | ||
assertThat(MyObjectFactory.allParams) | ||
.containsExactly(new Object[] {"foo"}, new Object[] {"bar"}); | ||
} | ||
|
||
private void testFactory(boolean onSuite, Class<?> sample) { | ||
MyObjectFactory.allParams.clear(); | ||
|
||
XmlSuite suite = createXmlSuite("Test IObjectFactory2", "TmpTest", sample); | ||
TestNG tng = create(suite); | ||
|
||
if (onSuite) { | ||
suite.setObjectFactoryClass(MyObjectFactory.class); | ||
} else { | ||
tng.setObjectFactory(MyObjectFactory.class); | ||
} | ||
|
||
tng.run(); | ||
} | ||
|
||
@DataProvider | ||
public static Object[][] dp() { | ||
return new Object[][] {new Object[] {true}, new Object[] {false}}; | ||
} | ||
} |
51 changes: 0 additions & 51 deletions
51
testng-core/src/test/java/test/objectfactory/github1131/GitHub1131Test.java
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
testng-core/src/test/java/test/objectfactory/github1827/GitHub1827Test.java
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
testng-core/src/test/java/test/objectfactory/issue2676/LocalSuiteAlteringListener.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,13 @@ | ||
package test.objectfactory.issue2676; | ||
|
||
import java.util.List; | ||
import org.testng.IAlterSuiteListener; | ||
import org.testng.xml.XmlSuite; | ||
|
||
public class LocalSuiteAlteringListener implements IAlterSuiteListener { | ||
|
||
@Override | ||
public void alter(List<XmlSuite> suites) { | ||
suites.forEach(each -> each.setObjectFactoryClass(LoggingObjectFactorySample.class)); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
testng-core/src/test/java/test/objectfactory/issue2676/LoggingObjectFactorySample.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,16 @@ | ||
package test.objectfactory.issue2676; | ||
|
||
import java.lang.reflect.Constructor; | ||
import org.testng.ITestObjectFactory; | ||
import org.testng.internal.objects.InstanceCreator; | ||
|
||
public class LoggingObjectFactorySample implements ITestObjectFactory { | ||
|
||
public static boolean wasInvoked = false; | ||
|
||
@Override | ||
public <T> T newInstance(Constructor<T> constructor, Object... parameters) { | ||
wasInvoked = true; | ||
return InstanceCreator.newInstance(constructor, parameters); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
testng-core/src/test/java/test/objectfactory/issue2676/TestClassSample.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,9 @@ | ||
package test.objectfactory.issue2676; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
public class TestClassSample { | ||
|
||
@Test | ||
public void sampleTestMethod() {} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use the same logic here? https://github.com/cbeust/testng/pull/2677/files#diff-73ef03ca958970a8d4a3f3c0764700a861f31f92a10d0b46930671bee51dfba8R98-R102
Maybe a private method could help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@juherr - I didnt understand this comment and the link you shared is taking me to the same class. Can you please help me understand ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was not explicit enough.
You have 2 different ways to create a dispenser in the same class. Is it expected? If it is expected, could you add a comment that explains why? If it is not expected, you can share a same builder method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@juherr - Yes you are right. One place is for the JUnit flow but I dont even see that codepath being executed and so I dont know why it exists even. I was first thinking that, this particular code path is going to get executed when we have
junit
attribute in suite set totrue
and I am running a bunch ofJUnit
tests using TestNG. But I noticed that the tests don't even take that path. It may be dead code but I cannot remove it until I know for sure. I will dig into it deeper separately and not in this PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From looking at the code, it looks like we perhaps never supported the notion of a test object factory for JUnit tests, because the codepath (when I execute a JUnit annotated test using TestNG) is going such that, JUnit is internally invoking the constructor of the test class and our objectfactory is not even coming into picture. I think if there are no other comments on this PR (which is to fix test objectfactory to be set for TestNG tests via listeners) we should merge this PR and deal with cleaning up the test object factory impact for JUnit tests separately (and maybe even remove the irrelevant code sections. )
Maybe I dont know enough but this is my first impression from what I see
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you ask me, I will propose to drop the junit support and advice to use https://github.com/junit-team/testng-engine when users ask for running both junit and testng.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@juherr sure. So can we go ahead with merging this PR ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just add a comment that link to the current discussion ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and captured this as a defect and included all the details #2683