如何在 Scalatest 中使用 MockitoSugar 和 AsyncFunSuite?
How to use MockitoSugar with AsyncFunSuite in Scalatest?
我无法将 MockitoSugar
与 AsyncFunSuite
一起使用。简而言之:
此示例有效(摘自 Scalatest documentation)
class AddSuite extends AsyncFunSuite {
def addSoon(addends: Int*): Future[Int] = Future { addends.sum }
test("addSoon will eventually compute a sum of passed Ints") {
val futureSum: Future[Int] = addSoon(1, 2)
futureSum map { sum => assert(sum == 3) }
}
}
但是这个例子没有(因为它有MockitoSugar
)
class AddSuite extends AsyncFunSuite with MockitoSugar {
def addSoon(addends: Int*): Future[Int] = Future { addends.sum }
test("addSoon will eventually compute a sum of passed Ints") {
val futureSum: Future[Int] = addSoon(1, 2)
futureSum map { sum => assert(sum == 3) }
}
}
后者均编译但报告零测试,而前者正确报告了一次测试。这在 IntelliJ 和 sbt 上都会发生。为什么这不起作用?我该如何解决这个问题?
我正在使用:
- scala 2.12
- scalatest 3.1.1
- 模拟核心 3.3.3
- mockito-scala 1.13.10
没关系,在查看 mockito-scala 代码库后弄明白了。我应该使用 AsyncMockitoSugar
而不是 MockitoSugar
。所以应该是:
class AddSuite extends AsyncFunSuite with AsyncMockitoSugar {
def addSoon(addends: Int*): Future[Int] = Future { addends.sum }
test("addSoon will eventually compute a sum of passed Ints") {
val futureSum: Future[Int] = addSoon(1, 2)
futureSum map { sum => assert(sum == 3) }
}
}
我无法将 MockitoSugar
与 AsyncFunSuite
一起使用。简而言之:
此示例有效(摘自 Scalatest documentation)
class AddSuite extends AsyncFunSuite {
def addSoon(addends: Int*): Future[Int] = Future { addends.sum }
test("addSoon will eventually compute a sum of passed Ints") {
val futureSum: Future[Int] = addSoon(1, 2)
futureSum map { sum => assert(sum == 3) }
}
}
但是这个例子没有(因为它有MockitoSugar
)
class AddSuite extends AsyncFunSuite with MockitoSugar {
def addSoon(addends: Int*): Future[Int] = Future { addends.sum }
test("addSoon will eventually compute a sum of passed Ints") {
val futureSum: Future[Int] = addSoon(1, 2)
futureSum map { sum => assert(sum == 3) }
}
}
后者均编译但报告零测试,而前者正确报告了一次测试。这在 IntelliJ 和 sbt 上都会发生。为什么这不起作用?我该如何解决这个问题?
我正在使用:
- scala 2.12
- scalatest 3.1.1
- 模拟核心 3.3.3
- mockito-scala 1.13.10
没关系,在查看 mockito-scala 代码库后弄明白了。我应该使用 AsyncMockitoSugar
而不是 MockitoSugar
。所以应该是:
class AddSuite extends AsyncFunSuite with AsyncMockitoSugar {
def addSoon(addends: Int*): Future[Int] = Future { addends.sum }
test("addSoon will eventually compute a sum of passed Ints") {
val futureSum: Future[Int] = addSoon(1, 2)
futureSum map { sum => assert(sum == 3) }
}
}