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?