Issue
versions:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
I am trying to use EqualsWithDelta like below, which is not working, however in similar way Equals works, is there something I am missing in my implementation:
import org.junit.Assert
import org.mockito.internal.matchers.{Equals, EqualsWithDelta}
val testVarLong = testFuncReturningLong()
val testVarStr = testFuncReturningString()
Assert.assertThat( System.currentTimeMillis(), new EqualsWithDelta(testVarLong, 1000L)) <-- This does not work
Assert.assertThat( "myTest", new Equals(testVarStr)) <-- This works
Following is compile time error I get:
Error:(82, 52) type mismatch;
found : org.mockito.internal.matchers.EqualsWithDelta
required: org.hamcrest.Matcher[_ >: Any]
Note: Number <: Any (and org.mockito.internal.matchers.EqualsWithDelta <: org.mockito.ArgumentMatcher[Number]), but Java-defined trait Matcher is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
Assert.assertThat( System.currentTimeMillis(), new EqualsWithDelta(testVarLong, 1000L))
Solution
new EqualsWithDelta(...)
is a Matcher[Number]
. System.currentTimeMillis()
is a Long
. Assert.assertThat
signature is assertThat(T actual, org.hamcrest.Matcher<T> matcher)
. So EqualsWithDelta
must be a subtype of Matcher<T>
and Long
must be a subtype of T
. The first implies T
must be Number
, but Long
isn't a subtype of Number
. So type inference reports such a T
doesn't exist.
However, if you ask for Number
explicitly in one of two ways:
assertThat[Number](System.currentTimeMillis(), new EqualsWithDelta(testVarLong, 1000L))
assertThat(System.currentTimeMillis(): Number, new EqualsWithDelta(testVarLong, 1000L))
this will trigger the implicit conversion from Long
to java.lang.Long
as a subtype of Number
.
Answered By - Alexey Romanov