JUnit to TestNG - @Rule in Superclass

时间:2015-12-07 15:56:41

标签: java junit testng

我正在从JUnit转换为TestNG,并面临一个远离JUnit中@Rule注释的问题。

在我的超类中有一个@Rule,当我将其更改为@BeforeMethod时,它根本不会进入该方法。 (使用Println确认)

另外,当我只是将@Rule或@ClassRule更改为@BeforeMethod时,Eclipse会抱怨它在这个位置被禁止了。

(注意我已经删除了下面的一些方法,以使代码长度更短/更容易查看)。

public abstract class AbstractTests
{

    protected static boolean oneTimeSetUpComplete;
    private static Logger log;
    private static WebSessionFactory sessionFactory;
    private static WcConfigManager config;

    private WebSession session;
    TestLogger testLogger = new TestLogger(getConfig(), getLog());

    private StringBuilder errors = new StringBuilder();

    protected WteDataProvider data;

    @ClassRule
    public static ExternalResource mainConfiguration = new ExternalResource() {

        protected void before() throws Throwable {
            setUpTests();
        };
    };

    public Verifier errorCollector = new Verifier(){
        @Override
        protected void verify() throws Throwable {
            failTestIfThereAreErrors();
        };
    };

    public TestLogger getTestLogger() {
        return testLogger;
    }

    @Rule
    public RuleChain executionOrder() {
        return RuleChain.outerRule(getTestLogger()).around(errorCollector); 
    }

    @Rule
    public TestRule dataReader = new TestRule() {

        public Statement apply(final Statement base,
                final Description description) {
            return new Statement() {
                @Override
                public void evaluate() throws Throwable {

                    System.out.println("PRINT IF YOU GO IN HERE");

                    DataProvider dataProvider = description.getAnnotation(DataProvider.class);
                    if (dataProvider == null) {
                        dataProvider = description.getTestClass().getAnnotation(DataProvider.class);
                    }

                    base.evaluate();
                };
            };
        };
    };

    protected AbstractArgosTests(
            Logger logger, 
            WcConfigManager configManager,
            WebSessionFactory factory)
    {
        setUpTests(logger, configManager, factory);
    }

}

0 个答案:

没有答案