Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

IInvokedMethodListener Iinvoked method does not return correct instance during @BeforeMethod, @AfterMethod and @AfterClass #3082

Closed
1 of 7 tasks
jalil29 opened this issue Mar 3, 2024 · 0 comments · Fixed by #3089

Comments

@jalil29
Copy link

jalil29 commented Mar 3, 2024

Trying to use IInvokedMethodListener to break reporting into different blocks for Selenium testing which is multithreaded and run in parallel.

TestNG Version

Version: 7.9.0

Expected behavior

During @BeforeMethod, @AfterMethod and @AfterClass the corresponding instance of test object to be returned via ITestResult.getInstance() or IInvokedMethod.getTestMethod().getInstance() is incorrect

Actual behavior

returns the wrong instance during @BeforeMethod, @AfterMethod and @AfterClass in IInvokedMethodListener listeners

Is the issue reproducible on runner?

  • Shell
  • Maven
  • Gradle
  • Ant
  • Eclipse
  • IntelliJ
  • NetBeans

Test case sample

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Parallel IconfigListener test">
    <test name="Parallel IConfigListener test" group-by-instances="true" parallel="instances">
        <classes>
            <class name="org.example.MainTest"/>
        </classes>
    </test>
</suite>

test class

import java.util.Arrays;
import java.util.HashMap;


@Listeners(value = {methodListener.class})
public class MainTest {

    public HashMap<String, Integer> hashMap = new HashMap<>();

    @Factory(dataProvider = "dataProvider")
    public MainTest() {
    }

    @DataProvider
    public static Object[][] dataProvider() {
        Object[][] objects = new Object[2][];
        Arrays.fill(objects, new Object[0]);
        return objects;
    }

    @BeforeClass
    public void beforeClass() {
        assertHashCode("beforeClass");
    }

    private void assertHashCode(String methodName) {
        Assert.assertEquals(hashMap.get(methodName), hashCode());
    }

    @BeforeMethod
    public void beforeMethod() {
        assertHashCode("beforeMethod");
    }

    @AfterMethod
    public void afterMethod() {
        assertHashCode("afterMethod");
    }

    @Test
    public void testMethod() {
        assertHashCode("testMethod");
    }

    @AfterClass
    public void afterClass() {
        assertHashCode("afterClass");
    }
}

Listener

package org.example;

import org.testng.*;

public class methodListener implements IInvokedMethodListener {

    @Override
    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
        String methodName = method.getTestMethod().getMethodName();
        MainTest instance = (MainTest) method.getTestMethod().getInstance();
        instance.hashMap.put(methodName, instance.hashCode());
    }
}
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants