我的HomeControllerSpec看起来像:
@RunWith(ClassOf[JUnitRunner])
class HomeControllerSpec extends Specification {
"HomeController" should {
object FakeGuiceGlobal extends play.api.GlobalSettings {
private lazy val injector = {
Guice.createInjector(new GuiceServicesModule)
}
override def getControllerInstance[A](clazz: Class[A]) = {
injector.getInstance(clazz)
}
override def onLoadConfig(config: Configuration, path: File, classloader: ClassLoader, mode: Mode.Mode): Configuration = {
val modeSpecificConfig = config ++ Configuration(ConfigFactory.load("application.test.conf"))
super.onLoadConfig(modeSpecificConfig, path, classloader, mode)
}
}
"index" in {
running(FakeApplication(withGlobal = Some(FakeGuiceGlobal))) {
// test here
}
}
}
}
由于某种原因,它无法加载application.test.conf
文件,但它仍在从默认的application.conf
文件中读取。
为什么不从测试配置文件中读取?
如果它不能'读取文件,如果文件路径不正确,为什么我没有看到错误?
答案 0 :(得分:1)
Play GlobalSettings.onLoadConfig
方法的文档说明:
在配置加载后调用,给出 申请修改它的机会。
提供该方法的覆盖不会让您有机会指定其他方法 要加载的配置,只能修改它。使用++运算符时,它的定义如下:
def ++(other: Configuration): Configuration = {
Configuration(other.underlying.withFallback(underlying))
}
请注意它如何调用other.underlying.withFallback
这是嵌入式Typesafe Config对象中的调用。从本质上讲,application.test.conf
的配置已加载并用作配置值的后备。在查找期间,您在原始文件中拥有的任何值都将被赋予优先级。只会在application.test.conf
中找到值。
仅供参考:"可信来源"这是播放代码本身。
答案 1 :(得分:0)
执行测试时,您需要覆盖默认位置:
-Dconfig.resource=./application.test.conf
确保文件路径正确。