2

If i declare a TogglzRule in an Unit-Test it must be public, otherwise there will be thrown an exception: java.lang.Exception: The @Rule 'togglzRule' must be public.

I used to declare members private, why is this not possible?

Cengiz
  • 5,375
  • 6
  • 52
  • 77

1 Answers1

6

That's a JUnit restriction. It's required to make injection of the rule work without doing any "dirty tricks". From the javadocs of @Rule:

Annotates fields that reference rules or methods that return a rule. A field must be public, not static, and a subtype of org.junit.rules.TestRule (preferred) or org.junit.rules.MethodRule.

chkal
  • 5,598
  • 21
  • 26