Skip to content
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

Unit tests for #2560 #2562

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions core/src/test/java/test/factory/github2560/ConstructorTest.java
@@ -0,0 +1,43 @@
package test.factory.github2560;

import org.testng.annotations.*;

public class ConstructorTest {
RiJo marked this conversation as resolved.
Show resolved Hide resolved

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 core/src/test/java/test/factory/github2560/FactoryTest.java
@@ -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 core/src/test/java/test/factory/github2560/Github2560Test.java
@@ -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");
}
}
@@ -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());
}
}
37 changes: 37 additions & 0 deletions core/src/test/java/test/factory/github2560/TestClass.java
@@ -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;
}
}
5 changes: 5 additions & 0 deletions core/src/test/resources/testng.xml
Expand Up @@ -441,6 +441,11 @@
<class name="test.factory.github2428.IssueTest" />
</classes>
</test>
<test name="factory test" group-by-instances="true">
<classes>
<class name="test.factory.github2560.Github2560Test" />
</classes>
</test>

<test name="TimeOut">
<classes>
Expand Down