问题
How can I create a FakeApplication to run my tests using my dev.conf instead of the default application.conf?
My current test consists of the following construct:
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
}
});
This code adapted from the Play Framework Documentation about writing test throws the following Exception, as the default application.conf does not work on my development-system. If I uncomment testServer.start(); I can see it even more clearly.
[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] ...
I assume the line
map.put("config.file", "/path/to/dev.conf");
is wrong and has to be adapted. But how?
回答1:
It is impossible to substitute main config with another this way. You only could override specific settings by passing a map to fakeApplication.
I.e. if you config contains:
mongodb.default.uri = ...
logger.root = ERROR
You can override it by placing new values inside a map:
Map<String, String> map = new HashMap<>();
map.put("mongodb.default.uri", "...");
map.put("logger.root", "INFO");
FakeApplication fakeApplication = fakeApplication(map);
回答2:
I think you might want to run your test command as follows:
play -Dconfig.file=path/to/dev.conf
test
回答3:
You may also want to check out this option:
http://alexgaribay.com/2015/02/05/overwrite-settings-for-testing-in-play-framework/
回答4:
I have the same issue and I did something like this to solve it:
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);
}
});
}
where application.dev.conf can be whaterver config file you want to set in there.
来源:https://stackoverflow.com/questions/14112753/fakeapplication-using-a-specific-application-conf