testng icon indicating copy to clipboard operation
testng copied to clipboard

@BeforeMethod in super classes depends on method visibility

Open matthauck opened this issue 12 years ago • 1 comments

See post on the user's group: https://groups.google.com/forum/#!topic/testng-users/fJd90c1RPWM

With a little bit of ad-hoc testing, it seems that if a superclass has a @BeforeMethod annotation, the sub-test-class only run it first (or even at all?) if it has protected visibility

import java.util.*;
import static org.testng.Assert.*;
import org.testng.annotations.*;

class SuperSampleTest {

    List<String> order = new ArrayList<>(2);

    @BeforeMethod
    public void superSetup() {
        System.out.println("running super");
        order.add("super");
    }

}

public class SampleTest extends SuperSampleTest {

    @BeforeMethod
    public void childSetup() {
        System.out.println("running child");
        order.add("child");
    }

    @Test 
    public void verifyOrder() {
        if (order.get(0).equals("child"))
            fail("child before method ran before super!");
    }

}

matthauck avatar Dec 27 '13 17:12 matthauck

I try to run it in my side and both super and child are run, the order is super and child

wutingbupt avatar Jan 31 '14 09:01 wutingbupt