Reputation: 15652
I'm learning Groovy as I develop this project so I'm not sure of my ground.
I have an assertion like so:
assertThat( spyCH.getLoopCount() ).isEqualTo( 1 )
There is no explicit method getLoopCount()
, but there is an instance variable loopCount
in the CH
class. Groovy automatically creates getters and setters.
I declared CH
instance variable loopCount
like so
def loopCount // i.e. "type undefined" (as yet)
Actually this got to the value 11. And I got the following fail:
org.junit.ComparisonFailure: expected:<1[]> but was:<1[1]>
Obviously the result is being interpreted as a String. I then changed the instance variable to
int loopCount
... but I still get the same String comparison
Then I changed the test line to:
assertThat( (int)spyCH.getLoopCount() ).isEqualTo( (int)1 )
... but I still get the same fail line.
Is there anyone out there who knows how you can force AssertJ to do an int
/Integer
comparison in Groovy? (NB they're the same in Groovy: it has no primitive values).
Upvotes: 0
Views: 547
Reputation: 7076
The JUnit comparison failure only shows the difference between the values represented as String
but the isEqualTo
assertion compares values according to their type ending up doing something like actual.equals(expected)
(or actual == expected) for primitive types
).
If you execute assertThat( spyCH.getLoopCount() ).isEqualTo( 456 )
the error will look like:
org.junit.ComparisonFailure: expected:<[1]> but was:<[456]>
Again a textual difference is shown but values are not compared as String.
When writing assertThat(value)
, java (and Groovy I believe) will use the best matching assertThat
methods, if value
is declared or inferred as an int
it will use assertThat(int actual)
, if the type is not specified then assertThat(Object actual)
is picked.
Hope that clarifies things a bit
Upvotes: 1