简体   繁体   中英

Scala and Mockito: keyword “then” is deprecated

Future versions of Scala may use then as a keyword, as stated in SIP-12 .

The compiler already shows a warning for it:

Usage of then as identifier is deprecated. It can be used as a keyword in future versions of scala. Word then is reserved as a possible keyword in future versions of scala. It's usage as identifier is deprecated. See SIP-12.

I'm using Mockito and have many occurrences of then methods called.

BDDMockito.then(entityService).should(Mockito.times(2)).findBy(any)

Does anyone know if there's an alternative to replace it?

In scala, wrapping any identifier in backticks "`" prevents it from being treated as a keyword.

BDDMockito.`then`(entityService).should(Mockito.times(2)).findBy(any)

This has most commonly been used to allow fields/variables/methods to be named type , but it should work for then as well. It's also sometimes used to embed spaces into identifiers.

case class Bar(`class`: Int, `type`: String) {
  def `class with type`: String = s"${`class`}_${`type`}"
}

Bar(42, "skidoo").`class with type` == "42_skidoo"

Have you tried mockito-scala ? its API is tailored for Scala and it caters for all the language features that the Java version doesn't

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM