Please post your Web Driver questions in official Web Driver forum

Friday, March 11, 2011

TestNG - DependsOnMethod which returns a value

Came across a bizarre scenario while using TestNG of late. I had test with method with a return type and another method being dependent on it. i.e. -

###########################################
public class TestNGTest {
   
    @Test
    public Integer returnsSomething() {
        return new Integer(0);
    }
   
    @Test(dependsOnMethods={"returnsSomething"})
    public void dependentMethod() {
        System.out.println("Hello World");
    }
}
###########################################

This over simple test does not work and throws exception -

###########################################
org.testng.TestNGException:
com.core.tests.TestNGTest.dependentMethod() is depending on nonexistent method com.core.tests.TestNGTest.returnsSomething
###########################################

And I wondered about this exception. Ended up in asking this to Cedric and his response was -

"Methods that return a value are ignored as test methods. Wrap it in a void
method and depend on that void method."

""Because it introduced a lot of false test methods for people using @Test
annotations at the class level."

Was not convinced with his thoughts but think that, any method which returns a value should be marked as data provider and not a test method. This separates data providers from test methods and provides better isolation of what should stay with which test method.
Fork me on GitHub