无法将测试配置注入 Play! 2.6 Guice创建的应用

Can't inject test Config into Play! 2.6 application created by Guice

在 Play! 的 JUnit 测试中! 2.6.25 应用程序在 IntelliJ 中执行,我试图将测试 config:Config 注入到由 Guice 创建的 app:Application 中。但是 app:Application 不包含注入的配置。我做错了什么?

Config config = ConfigFactory.parseFile(new File("test.conf")).resolve();
// myapp.key resolved to "value"
String value = config.getString("myapp.key");
GuiceApplicationBuilder guiceApplicationBuilder = new GuiceApplicationBuilder();
guiceApplicationBuilder.withConfigLoader(environment -> ConfigFactory.load(config));
Application app = guiceApplicationBuilder.build();
// why myapp.key cannot be resolved? 
app.config().getString("myapp.key");

test.conf

myapp.key = value

也尝试过(没有成功):

guiceApplicationBuilder.loadConfig(config);
guiceApplicationBuilder.configure(config);

您实际上是在丢弃具有自定义配置加载器的 Application Builder,因为该构建器是一个不可变的数据结构。这样做:

GuiceApplicationBuilder guiceApplicationBuilder = new GuiceApplicationBuilder()
  .withConfigLoader(environment -> ConfigFactory.load(config));

或者干脆

GuiceApplicationBuilder guiceApplicationBuilder = new GuiceApplicationBuilder()
  .configure(config);

在查看方法的签名时,您会注意到它们都是 return(一个新的!)GuiceApplicationBuilder.