我正在使用Scala中的Play 2.5应用程序,我正在使用Specs2进行测试。这是我的测试类:
class Entity1ServiceSpec extends Specification with Mockito {
private val entity1Repository = mock[Entity1Repository]
private val entity1Service: Entity1Service = new Entity1ServiceImpl(entity1Repository)
"Create AddedMessage" should {
"return true" in {
running(FakeAppBuilder.buildFakeApp) {
entity1Repository.create(0, "911", "01", 20) returns true
val result = entity1Service.create(0, "911", "01", 20)
there was one(entity1Repository).create(0, "911", "01", 20)
result shouldEqual true
}
}
"return false" in {
running(FakeAppBuilder.buildFakeApp) {
entity1Repository.create(1, "911", "01", 10) returns false
val result = entity1Service.create(1, "911", "01", 10)
there was one(entity1Repository).create(1, "911", "01", 10)
result shouldEqual false
}
}
}
}
当我在Intellij Idea下运行测试时,一切运行良好但是当我使用sbt test在终端下运行它时,它会失败并显示以下堆栈跟踪:
[error] cannot create an instance for class services.Entity1ServiceSpec
[error] caused by org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
[error] Misplaced argument matcher detected here:
[error]
[error] -> at org.specs2.mock.mockito.MockitoMatchers$class.any(MockitoMatchers.scala:45)
有人可以帮我弄清楚我做错了什么吗?
更新1:
@Singleton
class DefaultDBProvider @Inject() (val dbApi: DBApi) extends DBConnectionProvider {
override def withConnection[A](block: (Connection) => A): A =
dbApi.database("default").withConnection(block)
}
class Entity1RepositoryImpl @Inject() (val db: DefaultDBProvider) {
def create(some args) = {
db.withConnection { implicit conn => SQL("some sql").execute()
}
}
}
更新2: 我有另一个测试类,效果很好。然而,当我更换
val e2 = Entity2(any[Long], any[Long], "11111", any[String], any[String], any[ZonedDateTime])
在
val e2 = Entity2(0, 0, "11111", "111111", "text", ZonedDateTime.now())
Entity1ServiceSpec也开始运作良好。怎么可能,我所遇到的错误就是无法为类services.Entity1ServiceSpec创建实例,但实际问题是在Entity2ServiceSpec中?