Itchy Itchy - 2 months ago 10
Java Question

FakeApplication using a specific application.conf?

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?

Answer

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);