FakeApplication使用特定的application.conf?

时间:2013-01-01 17:51:34

标签: java playframework-2.0

如何使用FakeApplication代替默认dev.conf创建application.conf来运行测试?

我目前的测试包括以下构造:

Map<String, String> map = new HashMap<>();
map.put("config.file", "/path/to/dev.conf");
FakeApplication fakeApplication = fakeApplication(map);
TestServer testServer = testServer(3333, fakeApplication);
// testServer.start();
running(testServer, HTMLUNIT, new F.Callback<TestBrowser>() {
    public void invoke(TestBrowser browser) {
        //do something
    }
});

此代码改编自the Play Framework Documentation about writing test会引发以下异常,因为默认的application.conf不适用于我的开发系统。如果我取消注释testServer.start();,我可以更清楚地看到它。

[WARN] [01/01/2013 18:36:59.505] [pool-4-thread-3] [Dispatchers] Dispatcher [akka.actor.promises-dispatcher] not configured, using default-dispatcher
[WARN] [01/01/2013 18:36:59.521] [play-akka.actor.default-dispatcher-2] [Dispatchers] Dispatcher [akka.actor.actions-dispatcher] not configured, using default-dispatcher
[error] Test test.ApplicationTest.runInBrowser failed: Server is not started!
[error]     at scala.sys.package$.error(package.scala:27)
[error]     at play.api.test.TestServer.stop(Selenium.scala:117)
[error]     at play.test.Helpers.stop(Helpers.java:325)
[error]     at play.test.Helpers.running(Helpers.java:355)
[error]     at test.ApplicationTest.runInBrowser(ApplicationTest.java:74)
[error]     ...

我假设该行

map.put("config.file", "/path/to/dev.conf");

错了,必须进行调整。但是如何?

4 个答案:

答案 0 :(得分:9)

用这种方式替换主配置是不可能的。您只能通过将地图传递到fakeApplication来覆盖特定设置。

即。如果你配置包含:

mongodb.default.uri = ...
logger.root = ERROR

您可以通过在地图中放置新值来覆盖它:

Map<String, String> map = new HashMap<>();
map.put("mongodb.default.uri", "...");
map.put("logger.root", "INFO");

FakeApplication fakeApplication = fakeApplication(map);

答案 1 :(得分:0)

我想你可能想要运行你的测试命令,如下所示:

play -Dconfig.file=path/to/dev.conf test

答案 2 :(得分:0)

答案 3 :(得分:0)

我有同样的问题,我做了类似的事情来解决它:

  private Configuration additionalConfigurations;

  @Before
  public void initialize() {
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    Config additionalConfig  =ConfigFactory.parseFile(newFile(cl.getResource("application.dev.conf").getF)));
    additionalConfigurations = new Configuration(additionalConfig);
  }
  @Test
  public void testPropertiesGetLoaded() throws Exception{

    running(testServer(3333, fakeApplication(additionalConfigurations.asMap())), HTMLUNIT, new F.Callback<TestBrowser>(){
      public void invoke(TestBrowser browser) throws InterruptedException {

        String specificProperty = Play.application().configuration().getString("db.ro.url");
        Logger.info("printingVar:" + specificProperty);

      }
    });
  }

其中application.dev.conf可以是你想在那里设置的whaterver配置文件。