testng-team / testng

TestNG testing framework
https://testng.org
Apache License 2.0
1.99k stars 1.02k forks source link

IInvokedMethod isConfigurationMethod returning false for @BeforeXXX and @AfterXXX annotations #452

Closed scophotog closed 3 years ago

scophotog commented 11 years ago

I have created a listener class for my suite and have found that the isConfigurationMethod() is returning false for any of the @BeforeXXX and @AfterXXX annotations.

My snippet of test class:

    String url;
    @BeforeMethod
    public void setup() {
        this.url = "https://www.google.com";
    }

    @Test
    public void theTest() {        
        System.out.println(url);
    }

The listener class:

@Override
    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
        System.out.println("Configuration method? " + method.isConfigurationMethod()); // should return true for setup()

        System.out.println("BeforeMethod?> " + testResult.getMethod().isBeforeMethodConfiguration()); // returns true for setup()

    }

    @Override
    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
        System.out.println("Configuration method? " + method.isConfigurationMethod());  //should return true for setup

        System.out.println("BeforeMethod?> " + testResult.getMethod().isBeforeMethodConfiguration()); // returns true for setup()
    }
dzieciou commented 10 years ago

I have same issue.

Also isBeforeMethodConfiguration() is returning wrong value according to its JavaDoc

@return true if this method was annotated with @Configuration and beforeTestMethod = true

Your setup() method is not annotated with @Configuration, so isBeforeMethodConfiguration() should return false. This seems rather like a bug in documentation rather then in the code, because @Configuration annotation is deprecated:

@deprecated Use @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest, @BeforeGroups, @AfterGroups, @BeforeClass, @AfterClass, @BeforeMethod, @AfterMethod.

Are there any unit tests written for the framework itself? I could contribute here and they would catch such problems immediately.

VladRassokhin commented 9 years ago

@dzieciou you are right, it's about old @Configuration annotation. And unfortunately there not so much tests for framework itself. Contributions are welcome, especially with tests ;)

BTW I'd advise to drop @Configuration in testng-7.0 (why not?)

@scophotog try old good reflections ;) method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(BeforeMethod.class)

krmahadevan commented 3 years ago

This is no longer a problem with TestNG 7.3.0

Example:

import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners(LocalListener.class)
public class SampleTestClass {

  String url;

  @BeforeMethod
  public void setup() {
    this.url = "https://www.google.com";
  }

  @Test
  public void theTest() {
    Assert.assertNotNull(url);
  }
}
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;

public class LocalListener implements IInvokedMethodListener {

  @Override
  public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
    System.err.println("Inspecting " + method.getTestMethod().getQualifiedName());
    System.out.println("beforeInvocation: Configuration method? " + method
        .isConfigurationMethod()); // should return true for setup()

    System.out.println("beforeInvocation: BeforeMethod?> " + testResult.getMethod()
        .isBeforeMethodConfiguration()); // returns true for setup()

  }

  @Override
  public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
    System.err.println("Inspecting " + method.getTestMethod().getQualifiedName());
    System.out.println(
        "afterInvocation: Configuration method? " + method.isConfigurationMethod());  //should return true for setup

    System.out.println("BeforeMethod?> " + testResult.getMethod()
        .isBeforeMethodConfiguration()); // returns true for setup()
  }
}

Output:

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
Inspecting com.rationaleemotions.github.issue452.SampleTestClass.setup
beforeInvocation: Configuration method? true
beforeInvocation: BeforeMethod?> true

Inspecting com.rationaleemotions.github.issue452.SampleTestClass.setup
afterInvocation: Configuration method? true
BeforeMethod?> true

Inspecting com.rationaleemotions.github.issue452.SampleTestClass.theTest
beforeInvocation: Configuration method? false
beforeInvocation: BeforeMethod?> false
Inspecting com.rationaleemotions.github.issue452.SampleTestClass.theTest
afterInvocation: Configuration method? false
BeforeMethod?> false

===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0