Search code examples
togglz

Why must a TogglzRule be declared public?


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?


Solution

  • 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.