Using JUnit @Rule


#1

Hello,

I was trying to use JUnit Rules (https://github.com/junit-team/junit/wiki/Rules) with Kotlin, but I failed since the rule needs to be in a public variable.

import org.junit.Rule as rule
import org.junit.Test as test
import org.junit.rules.ErrorCollector

class RuleTest {
  rule
  val errorCollector: ErrorCollector = ErrorCollector()

  test
  fun myTest() {
  //something using the ErrorCollector
  //errorCollector.checkThat(“a String”, is(“a String”));
  }
}

Result:

java.lang.Exception: The @Rule ‘errorCollector’ must be public.
     at org.junit.internal.runners.rules.RuleFieldValidator.addError(RuleFieldValidator.java:116)
     at org.junit.internal.runners.rules.RuleFieldValidator.validatePublic(RuleFieldValidator.java:90)
     at org.junit.internal.runners.rules.RuleFieldValidator.validateMember(RuleFieldValidator.java:74)
     at org.junit.internal.runners.rules.RuleFieldValidator.validate(RuleFieldValidator.java:68)
     at org.junit.runners.BlockJUnit4ClassRunner.validateFields(BlockJUnit4ClassRunner.java:174)
     at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:105)
     at org.junit.runners.ParentRunner.validate(ParentRunner.java:355)
     at org.junit.runners.ParentRunner.<init>(ParentRunner.java:76)
     at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:57)
     at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10)
     at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
     at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
     at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
     at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
     at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:43)
     at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
     at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
     at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

How can I fix this? Adding a public keyword in front of the errorCollector does not help (as expected).

Thanks


#2

Right now there's no way to make a field public in Kotlin. Please file an issue for us to have a record of this use case. Thanks


#3

created http://youtrack.jetbrains.com/issue/KT-3441