1

I'm familiar with the necessity of Java unit tests making their @Rule properties public, however new to doing this in Scala. I understand that Scala classes have properties public by default, so expect this to be acceptable:

class MyTest {

  @Rule
  var failure: ExpectedException = ExpectedException.none()

}

What am I missing?

Charney Kaye
  • 3,667
  • 6
  • 41
  • 54
  • Okay, I'm in the middle of reading down https://stackoverflow.com/questions/32160549/using-junit-rule-with-scalatest-e-g-temporaryfolder which indicates that @Rule cannot be used in this way at all. – Charney Kaye Apr 28 '20 at 15:37

1 Answers1

1

The problem can be resolved by creating a member field of type ExpectedException and returning this field value by the @Rule function.

class MyTest {

  val _failure: ExpectedException = ExpectedException.none()

  @Rule
  def failure: ExpectedException = _failure

}
Charney Kaye
  • 3,667
  • 6
  • 41
  • 54