Search code examples
unit-testingjunitkotlinhamcrest

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


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?


Solution

  • In Kotlin, is is a reserved word . To get around this you need to escape the code using backticks, so the following will allow you to compile the code:

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