如何将配置实例注入scalatest?
我想在我的一个测试类中注入配置实例,我用ConfiguredApp扩展了我的测试类并注入了配置,它如下所示:如何将配置实例注入scalatest?,scala,playframework,playframework-2.0,guice,scalatest,Scala,Playframework,Playframework 2.0,Guice,Scalatest,我想在我的一个测试类中注入配置实例,我用ConfiguredApp扩展了我的测试类并注入了配置,它如下所示: @DoNotDiscover() class MyApiServiceSpec extends FreeSpec with ScalaFutures with ConfiguredApp { implicit val formats = DefaultFormats implicit val exec = global lazy val configuration =
@DoNotDiscover()
class MyApiServiceSpec extends FreeSpec with ScalaFutures with ConfiguredApp {
implicit val formats = DefaultFormats
implicit val exec = global
lazy val configuration = app.injector.instanceOf[Configuration]
"Global test" - {
"testcase 1" in {
Server.withRouter() {
case GET(p"/get/data") => Action { request =>
Results.Ok()
}
} { implicit port =>
WsTestClient.withClient { implicit client =>
val service = new MyApiService {
override def config: Configuration = configuration
override val ws: WSClient = client
}
whenReady(service.getData()) { res =>
//i will test stuff here
}
}
}
}
}
}
(MyApiService is a trait)
调用嵌套套件上的运行时遇到异常-
ConfiguredApp需要与密钥关联的应用程序值
配置图中的“org.scalatestplus.play.app”。你忘了吗
用@DoNotDiscover注释嵌套套件?
java.lang.IllegalArgumentException:ConfiguredApp需要一个应用程序
与配置中的键“org.scalatestplus.play.app”关联的值
地图。您是否忘记用@DoNotDiscover注释嵌套的套件
有人知道为什么
谢谢!333333我的答案不是当前问题的答案,但我想给出一些建议。如果您想为控制器或某些服务编写单元测试,我建议使用PlaySpec。为了为测试环境注入自定义配置:
class MyControllerSpec extends PlaySpec with OneAppPerSuite {
val myConfigFile = new File("app/test/conf/application_test.conf")
val parsedConfig = ConfigFactory.parseFile(myConfigFile)
val configuration = ConfigFactory.load(parsedConfig)
implicit override lazy val app: Application = new GuiceApplicationBuilder()
.overrides(bind[Configuration].toInstance(Configuration(configuration)))
.build()
"MyController #index" should {
"should be open" in {
val result = route(app, FakeRequest(GET, controllers.routes.MyController.index().url)).get
status(result) mustBe OK
}
}
}
似乎您试图单独运行此测试。但是使用
ConfiguredApp
时,必须使用套件运行此测试,如
class AcceptanceSpecSuite extends PlaySpec with GuiceOneAppPerSuite {
override def nestedSuites = Vector(new MyApiServiceSpec)
}
注射看起来正常。疑似相似: