我正在尝试使用 hamcrest 和 <
测试整数数组的长度import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.collection.IsArrayWithSize.arrayWithSize
import org.hamcrest.core.IsEqual.equalTo
import org.hamcrest.core.Is.`is` as Is
…
val parts = intArrayOf(1, 2, 3)
assertThat(parts, arrayWithSize(3))
assertThat(parts, Is(arrayWithSize(3)))
assertThat(parts, Is(arrayWithSize(equalTo(3))))
据我所知,所有三个断言都应该有效,并且等效的语句也可以在 Java 中工作。但它们不能与 Kotlin 一起使用:
[ERROR] …/Decomposed_Test.kt:[287,14] Type mismatch: inferred type is IntArray but String! was expected
[ERROR] …/Decomposed_Test.kt:[287,21] Type mismatch: inferred type is Matcher<Array<(???..???)>!>! but Boolean was expected
[ERROR] …/Decomposed_Test.kt:[288,14] Type mismatch: inferred type is IntArray but String! was expected
[ERROR] …/Decomposed_Test.kt:[288,21] Type mismatch: inferred type is Matcher<(???..???)>! but Boolean was expected
[ERROR] …/Decomposed_Test.kt:[289,14] Type mismatch: inferred type is IntArray but String! was expected
[ERROR] …/Decomposed_Test.kt:[289,21] Type mismatch: inferred type is Matcher<(???..???)>! but Boolean was expected
这当然是错误的,因为我没有比较字符串。我尝试在断言中添加一些原因以查看是否有帮助:
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.collection.IsArrayWithSize.arrayWithSize
import org.hamcrest.core.IsEqual.equalTo
import org.hamcrest.core.Is.`is` as Is
…
val parts = intArrayOf(1, 2, 3)
assertThat(/* reason = */ "array has 3 elements", /* actual = */ parts, /* matcher = */ arrayWithSize(3))
assertThat(/* reason = */ "array has 3 elements", /* actual = */ parts, /* matcher = */ Is(arrayWithSize(3)))
assertThat(/* reason = */ "array has 3 elements", /* actual = */ parts, /* matcher = */ Is(arrayWithSize(equalTo(3))))
不同的错误消息,但仍然没有帮助:
[ERROR] …/Decomposed_Test.kt:[287,91] Type mismatch: inferred type is IntArray! but Array<(out) TypeVariable(E)!>! was expected
[ERROR] …/Decomposed_Test.kt:[288,91] Type mismatch: inferred type is IntArray! but Array<(out) TypeVariable(E)!>? was expected
[ERROR] …/Decomposed_Test.kt:[288,91] Type mismatch: inferred type is IntArray! but Array<(out) TypeVariable(E)!>! was expected
[ERROR] …/Decomposed_Test.kt:[288,94] Type mismatch: inferred type is Matcher<Array<(out) (???..???)>!>! but Matcher<IntArray!>! was expected
[ERROR] …/Decomposed_Test.kt:[289,91] Type mismatch: inferred type is IntArray! but Array<(out) TypeVariable(E)!>? was expected
[ERROR] …/Decomposed_Test.kt:[289,91] Type mismatch: inferred type is IntArray! but Array<(out) TypeVariable(E)!>! was expected
[ERROR] …/Decomposed_Test.kt:[289,94] Type mismatch: inferred type is Matcher<Array<(out) (???..???)>!>! but Matcher<IntArray!>! was expected
这是为什么?
PS:我对替代框架的建议不感兴趣。
回答我自己的问题(就像我经常做的那样),正确的代码是:
val parts = intArrayOf(1, 2, 3)
assertThat(/* actual = */ parts.toTypedArray(), /* matcher = */ arrayWithSize(3))
assertThat(/* actual = */ parts.toTypedArray(), /* matcher = */ Is(arrayWithSize(3)))
assertThat(/* actual = */ parts.toTypedArray(), /* matcher = */ Is(arrayWithSize(equalTo(3))))
这种转换是性能杀手,但在单元测试中我不在乎。如果我曾经将主要源代码从 java 转换为 kotlin 这样的东西可能会受到伤害。