Skip to content

Commit

Permalink
Unit tests for testng-team#2560
Browse files Browse the repository at this point in the history
  • Loading branch information
RiJo committed May 27, 2021
1 parent e6e6e74 commit edbf334
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
43 changes: 43 additions & 0 deletions core/src/test/java/test/factory/github2560/ConstructorTest.java
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 core/src/test/java/test/factory/github2560/FactoryTest.java
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 {

@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
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");
}
}
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());
}
}
37 changes: 37 additions & 0 deletions core/src/test/java/test/factory/github2560/TestClass.java
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 {

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
Original file line number Diff line number Diff line change
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

0 comments on commit edbf334

Please sign in to comment.