Skip to content

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

@jalil29

Description

@jalil29

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());
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions