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

3

34

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?

Adenoidectomy answered 13/10, 2016 at 16:5 Comment(0)
A
42

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))
  }
}
Adenoidectomy answered 13/10, 2016 at 16:5 Comment(1)
You might be able to make use of infix operator maybe to allow assertThat(1 is equalTo(1))... but you would have to wrap a good bit I think. Maybe with the combination of some DSL style work you get to the point one can write assertThat{ N isEqualTo 1} but I'm not inclined to put that effort in... yetAdenoidectomy
M
46

You can alias is to Is (for example) in the import statement using the as keyword.

For example:

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

See https://kotlinlang.org/docs/packages.html#imports

Mickey answered 14/10, 2016 at 11:34 Comment(4)
You would still need to reference the is function when you import it, so you would still need to escape it. But this is a nice trick that would help save you from escaping it all over the place.Adenoidectomy
That is true, just once, something like: import org.hamcrest.CoreMatchers.is as IsMickey
At which point it's just a style thing. Personally, I'd rather escape the function name personally.Adenoidectomy
Up to you. With 'aliasing' you need to this only onceMickey
A
42

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))
  }
}
Adenoidectomy answered 13/10, 2016 at 16:5 Comment(1)
You might be able to make use of infix operator maybe to allow assertThat(1 is equalTo(1))... but you would have to wrap a good bit I think. Maybe with the combination of some DSL style work you get to the point one can write assertThat{ N isEqualTo 1} but I'm not inclined to put that effort in... yetAdenoidectomy
I
16

As others pointed out, in Kotlin, is is a reserved word (see Type Checks). But it's not a big problem with Hamcrest since is function is just a decorator. It's used for better code readability, but it's not required for proper functioning.

You are free to use a shorter Kotlin-friendly expression.

  1. equality:

    assertThat(cheese, equalTo(smelly))
    

    instead of:

    assertThat(cheese, `is`(equalTo(smelly)))
    
  2. matcher decorator:

    assertThat(cheeseBasket, empty())
    

    instead of:

    assertThat(cheeseBasket, `is`(empty()))
    

Another frequently used Hamcrest matcher is a type-check like

assertThat(cheese, `is`(Cheddar.class))

It's deprecated and it's not Kotlin-friendly. Instead, you're advised to use one of the following:

assertThat(cheese, isA(Cheddar.class))
assertThat(cheese, instanceOf(Cheddar.class))
Incompressible answered 20/3, 2019 at 12:13 Comment(1)
Oh, this is actually quite nice to know. Never knew it was basically a redundant extra call.Adenoidectomy

© 2022 - 2024 — McMap. All rights reserved.