The @Rule > must be public ValidationError in Kotlin Junit test

Viewed 6274

I tried to use a unit test rule annotation and Android Studio didn't highlight any error here:

@Rule val htmlManager = HtmlManager()

However after executing the test following error happens:

org.junit.internal.runners.rules.ValidationError: The @Rule 'htmlManager' must be public.

How to fix this?

3 Answers

You can also use the @JvmField annotation

@Rule @JvmField 
val htmlManager = HtmlManager()

Another solution is:

val htmlManager = HtmlManager()
    @Rule get() = field
Related