How to compile Kotlin unit test code that uses hamcrest 'is'

thecoshman picture thecoshman · Oct 13, 2016 · Viewed 9.3k times · Source

I want to write a unit test for my Kotlin code and use junit/hamcrest matchers, I want to use the is method, but it is a reserved word in Kotlin.

How can I get the following to compile?

class testExample{
  @Test fun example(){
    assertThat(1, is(equalTo(1))
  }
}

Currently my IDE, InteliJ is highlighting that as a compilation error, saying it is expecting a ) after is?

Answer

David Soroko picture David Soroko · Oct 14, 2016

You can alias is to Is (for example), when you import, using the as keyword.

E.g:

 import org.hamcrest.CoreMatchers.`is` as Is

See https://kotlinlang.org/docs/reference/packages.html