小编wal*_*lak的帖子

如何更改功能测试的Guice绑定?

我正在使用Guice作为DI提供商的Play(v.4.4)应用程序.一切运行正常,但我有一组运行ScalaTestPlus的功能测试,我想在测试运行时替换一些依赖项.测试是通过OneServerPerSuite在检查我的REST API时扩展类来编写的.

有没有办法在测试期间有其他依赖项?

编辑:示例代码:

样品控制器:

class UserController @Inject()(userService: UserService) extends AbstractController { ... }
Run Code Online (Sandbox Code Playgroud)

模块中的dependecy定义:

bind(classOf[UserService]) to (classOf[ProdUserService])
Run Code Online (Sandbox Code Playgroud)

我的测试是这样的:

class ApiTest extends PlaySpec with OneServerPerSuite {

    "User API should" must {
         "get User's data" in {
             (...) //calling to an endpoint and verifying response
         }
     }
}
Run Code Online (Sandbox Code Playgroud)

我想ProdUserService用其他实现替换但仅在测试中.

testing scala guice playframework

6
推荐指数
1
解决办法
441
查看次数

标签 统计

guice ×1

playframework ×1

scala ×1

testing ×1