0

I'm converting from JUnit to TestNG and facing a issue moving away from the @Rule annotation in JUnit.

In my superclass there is a @Rule, when I change this to @BeforeMethod it simply does not go into that method. (using that Println to confirm)

Also, when i do simply change @Rule or @ClassRule to @BeforeMethod, Eclipse complains that it is 'disallowed at this location'.

(Note I've removed some methods in the below to make the code length a little shorter/easier to view).

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);
    }

}
userMod2
  • 8,312
  • 13
  • 63
  • 115

0 Answers0