Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我在单元测试中声明 TogglzRule 它必须是公共的,否则会抛出异常:java.lang.Exception: The @Rule 'togglzRule' must be public.
java.lang.Exception: The @Rule 'togglzRule' must be public.
我曾经将成员声明为私有,为什么这不可能?
这是一个 JUnit 限制。需要在不做任何“肮脏技巧”的情况下注入规则。来自的javadocs @Rule:
@Rule
注释引用规则或返回规则的方法的字段。字段必须是公共的,而不是静态的,并且是org.junit.rules.TestRule(首选)或的子类型org.junit.rules.MethodRule。
org.junit.rules.TestRule
org.junit.rules.MethodRule