1

I am trying following code:

public class ShashiTest {
    @Test
    public void test1(){
        System.out.println("1===========");
    }

    @Test(dependsOnMethods="test1")
    public void test2(){
        System.out.println("2===========");
    }

    @Test(dependsOnMethods="test2")
    public void test3(){
        System.out.println("3===========");
    }

    @AfterMethod(dependsOnMethods={"test2","test3"})
    public void test4(){
        System.out.println("4===========");
    }
}

I am expecting output as:

1===========
2===========
4===========
3===========
4===========

But I am getting exception as test method not found:

com.ShashiTest.test4() is depending on method public void com.ShashiTest.test2(), which is not annotated with @Test or not included.
    at org.testng.internal.MethodHelper.findDependedUponMethods(MethodHelper.java:111)

Where I am making the mistake? How I can achieve my goal?

Shashi Ranjan
  • 1,491
  • 6
  • 27
  • 52
  • Please see Julien Herr's answer below. A quick observation: once you start depending on multiple methods, I recommend depending on groups instead, which is much easier to maintain. But you definitely seem to have found a bug here. – Cedric Beust Aug 05 '16 at 15:23

3 Answers3

2

@AfterMethod declares that this method is run after every method annotated with @Test. Right now you have a conflict with test4() being called after test1() and before test2(), while also requiring it to be run after test2(). Refer to this for more in-depth discussion.

edit: I should probably make the call order more clear.

test1()->test4()
test2()->test4()
test3()->test4()

As you can see, requiring test4() to be run after test2() and test3() is in conflict with the @AfterMethod annotation requiring it be called after every method.

John K
  • 462
  • 2
  • 11
  • oh! Probably I need to go thru these attribute more in details. @JohnK Basically I do not want to write test4() method twice and I want to run test4() after test2() and test3() but not after test1() . Any idea how I can get this? – Shashi Ranjan Aug 04 '16 at 05:26
  • For something like this I think the best answer is to just have test2 and test3 manually call test4 as the last line in the method. @nick-humrich has an excellent explanation of the test set-ups and tear downs [here](http://stackoverflow.com/a/20642025/5273975) – John K Aug 04 '16 at 05:35
  • It's generally a bad practice for unit tests to rely on execution order. All the unit tests must be self contained. http://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4 – Anupam Saini Aug 04 '16 at 05:37
1

dependsOnMethod is not working like that and just used to order methods between them. The javadoc is clear enough IMO:

The list of methods this method depends on. There is no guarantee on the order on which the methods depended upon will be run, but you are guaranteed that all these methods will be run before the test method that contains this annotation is run. Furthermore, if any of these methods was not a SUCCESS, this test method will not be run and will be flagged as a SKIP. If some of these methods have been overloaded, all the overloaded versions will be run.

But the exception should not happen so I opened an issue for it.

About your need which is running an @AfterMethod for specific methods only (something looks weird, but why not), you can do the following:

public class ShashiTest {
    @Test
    public void test1(){
        System.out.println("1===========");
    }

    @Test(dependsOnMethods="test1")
    public void test2(){
        System.out.println("2===========");
    }

    @Test(dependsOnMethods="test2")
    public void test3(){
        System.out.println("3===========");
    }

    @AfterMethod
    public void test4(Method m){
        switch(m.getName()) {
            case "test2":
            case "test3": 
                System.out.println("4===========");    
        }
    }
}

should work as expected.

juherr
  • 5,640
  • 1
  • 21
  • 63
0

Bit late to answer but I just faced this problem today. Error: com.expedia.FlightBooking.tearDown() is depending on method public void com.expedia.FlightBooking.flightBooking(), which is not annotated with @Test or not included.

Solution: Changing dependsOnMethods to dependsOnGroups Ex: @AfterTest(dependsOnGroups = {"flightBooking"}) has solved my problem.

Vicky
  • 13
  • 3