0

Assuming you have a TestRule (for example, a TemporaryFolder) defined, you can apply the rule in a test class by either way below:

1.By annotating a method with @Rule

public MyTestClass{
  private TemporaryFolder folder= new TemporaryFolder();
  @Rule
  public TemporaryFolder getFolder() {
    return folder;
  }
  @Test
  ...
}

2.By annotating a field with @Rule

public MyTestClass{
  @Rule
  public TemporaryFolder folder= new TemporaryFolder();
  @Test
  ...
}

It seems that every test case invokes the TemporaryFolder constructor in both forms. What's the pros/cons of each form?

sunrise
  • 195
  • 1
  • 11

1 Answers1

1

The second version is more concise, otherwise they are identical. Allowing methods to be annotated by Rule was added for other JVM-based languages.

NamshubWriter
  • 23,549
  • 2
  • 41
  • 59