1

I have a rule defined for "TemporaryFolder" as below:

@Rule
public TemporaryFolder xyzFolder = new TemporaryFolder();

and in method:

private void testMethod() {
    File testFile = xyzFolder.newFile("test");
}

But getting exception as:

java.lang.Exception: The @Rule 'xyzFolder' must implement MethodRule or TestRule.
swetha
  • 449
  • 7
  • 20
  • That sounds like a ClassLoader conflict. Do you have more than one version of JUnit in your project? – Sean Patrick Floyd May 03 '17 at 10:09
  • 1
    Please show us a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). With the given information we may not be able to find a solution to your issue, especially if you don't include such information in your OP that the method is invoked from within the actual test-method and similar stuff – Roman Vottner May 03 '17 at 10:36

2 Answers2

0

A test method should be public to be run as a test case and to correctly interpret the Rule.

 @Test
 public void testMethod() {
     File testFile = xyzFolder.newFile("test");
 }

http://junit.org/junit4/javadoc/4.12/org/junit/Test.html

Without seing more details of your code, however, the only other thing I can suggest is to check if you are using the correct version of JUnit and its org.junit.rules.TemporaryFolder and not something else.

Spock
  • 315
  • 2
  • 13
0

Add "implements TestRule" or "implements MethodRule" to your Class xyzFolder Like "xyzFolder implements TestRule/MethodRule" . That shall solve the error, and later you have to implement the unimplemented methods according to your needs, Have a look at this for some more details https://junit.org/junit4/javadoc/4.12/org/junit/rules/MethodRule.html .

Akhil KC
  • 76
  • 1
  • 8