-
Notifications
You must be signed in to change notification settings - Fork 1k
Unit tests for #2560 #2562
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
Draft
RiJo
wants to merge
3
commits into
testng-team:master
Choose a base branch
from
RiJo:issue-2560
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Unit tests for #2560 #2562
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
43 changes: 43 additions & 0 deletions
43
core/src/test/java/test/factory/github2560/ConstructorTest.java
This file contains hidden or 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,43 @@ | ||
package test.factory.github2560; | ||
|
||
import org.testng.annotations.*; | ||
|
||
public class ConstructorTest { | ||
|
||
private final int hashCode; | ||
|
||
@Factory(dataProvider = "constructorArguments") | ||
public ConstructorTest(int hashCode) { | ||
this.hashCode = hashCode; | ||
} | ||
|
||
@DataProvider | ||
public static Object[][] constructorArguments() { | ||
return new Object[][]{{0}, {1}, {2}}; | ||
} | ||
|
||
@BeforeClass | ||
public void beforeClass() { | ||
} | ||
|
||
@BeforeMethod | ||
public void beforeMethod() { | ||
} | ||
|
||
@Test | ||
public void test() { | ||
} | ||
|
||
@AfterMethod | ||
public void afterMethod() { | ||
} | ||
|
||
@AfterClass | ||
public void afterClass() { | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return hashCode; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
core/src/test/java/test/factory/github2560/FactoryTest.java
This file contains hidden or 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,15 @@ | ||
package test.factory.github2560; | ||
|
||
import org.testng.annotations.Factory; | ||
|
||
public class FactoryTest { | ||
RiJo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Factory | ||
public static Object[] factory() { | ||
return new Object[]{ | ||
new TestClass(0), | ||
new TestClass(1), | ||
new TestClass(2) | ||
}; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
core/src/test/java/test/factory/github2560/Github2560Test.java
This file contains hidden or 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,47 @@ | ||
package test.factory.github2560; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import org.testng.Assert; | ||
import org.testng.TestNG; | ||
import org.testng.annotations.Test; | ||
import test.SimpleBaseTest; | ||
|
||
public class Github2560Test extends SimpleBaseTest { | ||
|
||
@Test | ||
public void staticFactory() { | ||
TestNG testng = create(FactoryTest.class); | ||
testng.setDefaultSuiteName("Static @Factory tests"); | ||
InvokedMethodListener invokedMethodListener = new InvokedMethodListener(); | ||
testng.addListener(invokedMethodListener); | ||
|
||
testng.run(); | ||
|
||
ImmutableMap<Integer, ImmutableList<String>> expected = ImmutableMap.of( | ||
0, ImmutableList.of("beforeClass", "beforeMethod", "test", "afterMethod", "afterClass"), | ||
1, ImmutableList.of("beforeClass", "beforeMethod", "test", "afterMethod", "afterClass"), | ||
2, ImmutableList.of("beforeClass", "beforeMethod", "test", "afterMethod", "afterClass") | ||
); | ||
Assert.assertEquals(invokedMethodListener.capturedBeforeInvocations, expected, "beforeInvocation"); | ||
Assert.assertEquals(invokedMethodListener.capturedAfterInvocations, expected, "afterInvocation"); | ||
} | ||
|
||
@Test | ||
public void constructorFactory() { | ||
TestNG testng = create(ConstructorTest.class); | ||
testng.setDefaultSuiteName("Constructor @Factory tests"); | ||
InvokedMethodListener invokedMethodListener = new InvokedMethodListener(); | ||
testng.addListener(invokedMethodListener); | ||
|
||
testng.run(); | ||
|
||
ImmutableMap<Integer, ImmutableList<String>> expected = ImmutableMap.of( | ||
0, ImmutableList.of("beforeClass", "beforeMethod", "test", "afterMethod", "afterClass"), | ||
1, ImmutableList.of("beforeClass", "beforeMethod", "test", "afterMethod", "afterClass"), | ||
2, ImmutableList.of("beforeClass", "beforeMethod", "test", "afterMethod", "afterClass") | ||
); | ||
Assert.assertEquals(invokedMethodListener.capturedBeforeInvocations, expected, "beforeInvocation"); | ||
Assert.assertEquals(invokedMethodListener.capturedAfterInvocations, expected, "afterInvocation"); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
core/src/test/java/test/factory/github2560/InvokedMethodListener.java
This file contains hidden or 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,28 @@ | ||
package test.factory.github2560; | ||
|
||
import org.testng.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
public class InvokedMethodListener implements IInvokedMethodListener { | ||
|
||
final Map<Integer, List<String>> capturedBeforeInvocations = new ConcurrentHashMap<>(); | ||
final Map<Integer, List<String>> capturedAfterInvocations = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void beforeInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) { | ||
Assert.assertSame(method.getTestMethod().getInstance(), testResult.getInstance()); | ||
capturedBeforeInvocations.computeIfAbsent(testResult.getInstance().hashCode(), ignored -> new ArrayList<>()) | ||
.add(method.getTestMethod().getMethodName()); | ||
} | ||
|
||
@Override | ||
public void afterInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) { | ||
Assert.assertSame(method.getTestMethod().getInstance(), testResult.getInstance()); | ||
capturedAfterInvocations.computeIfAbsent(testResult.getInstance().hashCode(), ignored -> new ArrayList<>()) | ||
.add(method.getTestMethod().getMethodName()); | ||
} | ||
} |
This file contains hidden or 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,37 @@ | ||
package test.factory.github2560; | ||
|
||
import org.testng.annotations.*; | ||
|
||
public class TestClass { | ||
RiJo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private final int hashCode; | ||
|
||
public TestClass(int hashCode) { | ||
this.hashCode = hashCode; | ||
} | ||
|
||
@BeforeClass | ||
public void beforeClass() { | ||
} | ||
|
||
@BeforeMethod | ||
public void beforeMethod() { | ||
} | ||
|
||
@Test | ||
public void test() { | ||
} | ||
|
||
@AfterMethod | ||
public void afterMethod() { | ||
} | ||
|
||
@AfterClass | ||
public void afterClass() { | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return hashCode; | ||
} | ||
} |
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.