AssertJ和Groovy强制类型?

AssertJ and Groovy force type?

我在开发这个项目时正在学习 Groovy,所以我不确定自己的立场。

我有这样的断言:

assertThat( spyCH.getLoopCount() ).isEqualTo( 1 )

没有显式方法getLoopCount(),但是在CHclass中有一个实例变量loopCount。 Groovy 自动创建 getter 和 setter。

我这样声明了CH实例变量loopCount

def loopCount // i.e. "type undefined" (as yet)

实际上这得到了值 11。我得到了以下失败:

org.junit.ComparisonFailure: expected:<1[]> but was:<1[1]>

显然结果被解释为字符串。然后我将实例变量更改为

int loopCount

...但我仍然得到相同的字符串比较

然后我把测试线改成了:

assertThat( (int)spyCH.getLoopCount() ).isEqualTo( (int)1 )

...但我仍然遇到相同的失败行。

有没有人知道如何强制 AssertJ 在 Groovy 中进行 int/Integer 比较? (注意它们在 Groovy 中是相同的:它没有原始值)。

JUnit 比较失败仅显示值之间的差异 表示StringisEqualTo 断言根据它们的类型比较值类似于 actual.equals(expected)(或 actual == expected) for primitive types)。

如果您执行 assertThat( spyCH.getLoopCount() ).isEqualTo( 456 ),错误将如下所示:

org.junit.ComparisonFailure: expected:<[1]> but was:<[456]>

再次显示文本差异,但不将值作为字符串进行比较。

当写 assertThat(value) 时,java(我相信 Groovy)将使用最匹配的 assertThat 方法,如果 value 被声明或推断作为 int,它将使用 assertThat(int actual),如果未指定类型,则选择 assertThat(Object actual)

希望能澄清一些事情