Как использовать EqualsWithDelta из junit - PullRequest
0 голосов
/ 15 февраля 2019

версии:

<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>

Я пытаюсь использовать EqualsWithDelta , как показано ниже, который не работает, однако аналогичным образом Равен работает, есть что-тоЯ отсутствует в моей реализации:

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

Ниже приводится ошибка времени компиляции, которую я получаю:

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))

Ответы [ 2 ]

0 голосов
/ 25 февраля 2019

new EqualsWithDelta(...) является Matcher[Number].System.currentTimeMillis() является Long.Assert.assertThat подпись assertThat(T actual, org.hamcrest.Matcher<T> matcher).Таким образом, EqualsWithDelta должен быть подтипом Matcher<T>, а Long должен быть подтипом T.Первое подразумевает, что T должно быть Number, но Long не является подтипом Number.Поэтому отчеты о выводе типа, такие как T, не существуют.

Однако, если вы явно запросите Number одним из двух способов:

assertThat[Number](System.currentTimeMillis(), new EqualsWithDelta(testVarLong, 1000L))

assertThat(System.currentTimeMillis(): Number, new EqualsWithDelta(testVarLong, 1000L))

, это вызовет неявное преобразование из Long в java.lang.Long в качестве подтипа Number.

0 голосов
/ 21 февраля 2019

Попробуйте привести

assertThat( System.currentTimeMillis(), new EqualsWithDelta(testVarLong, 1000L).asInstanceOf[Matcher[_ >: Long]])

или просто

assertThat[Long]( System.currentTimeMillis(), new EqualsWithDelta(testVarLong, 1000L).asInstanceOf[Matcher[Long]])

В любом случае, во время выполнения new EqualsWithDelta... это просто Matcher[_] из-за стирания типа, так что это приведениеsafe. Вы должны следовать совету @ AlexeyRomanov.

Ограничения типа Scala и универсальное взаимодействие Java

...