Help the world stop coronavirus! Stay home!

Prev Next

JUnit / Junit Interview questions III

Junit: What is @Rule annotation?

@Rule annotation in Junit is used to create objects that later can be used with the test methods.

Junit @Rule annotation works on the principle of AOP (aspect oriented programming). It intercepts the test method thus providing an opportunity perform some action before or after the execution of a particular test method.

public class JunitRuleExampleTest {

  public TemporaryFolder tFolder = new TemporaryFolder();

  public void testRule() throws IOException {
    File txtFile = tFolder.newFile("sample.txt");

Every time the above test method is executed, a temporary folder is created and used in the test while executing and be deleted/garbage collected after the test run.

❤Cash Back At Stores you Love !!!❤

Earn your $10 reward when you make your first purchase through Ebates by signing up with clicking below button.

Ebates Coupons and Cash Back

More Related questions...

Show more question and Answers...


Comments & Discussions