如何编译使用hamcrest'is'的Kotlin单元测试代码 [英] How to compile Kotlin unit test code that uses hamcrest 'is'
本文介绍了如何编译使用hamcrest'is'的Kotlin单元测试代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为我的Kotlin代码编写一个单元测试,并使用junit/hamcrest匹配器,我想使用is
方法,但这是Kotlin中的保留字.
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))
}
}
当前,我的IDE中,InteliJ突出显示这是一个编译错误,说它期望在is
之后出现)
?
Currently my IDE, InteliJ is highlighting that as a compilation error, saying it is expecting a )
after is
?
推荐答案
在科特林,is
是保留字.要解决此问题,您需要使用反引号对代码进行转义,因此以下内容将允许您编译代码:
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))
}
}
这篇关于如何编译使用hamcrest'is'的Kotlin单元测试代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文