Does TestNG support something like JUnit4’s @Rule?

Rules are pretty easy to emulate, for example with super classes:

public void Base {
  @BeforeMethod
  public void createTempDir() { ... }

  @AfterMethod
  public void deleteTempDir() { ... }
}

public void MyTest extends Base {
  @Test
  ...
}

If you extend Base, a temporary directory will always be automatically created and then deleted.

The advantage of this approach over Rules is that Rules are always class scoped, while with TestNG, you can implement them around methods, tests, classes, groups and even suites.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)