Play 2.0 FakeApplication setup with test configuration

cmacher picture cmacher · Sep 3, 2012 · Viewed 12.9k times · Source

I have a specs2 test which uses a FakeApplication and an embedded mongodb database.

def inMemoryMongoDatabase(name: String = "default"): Map[String, String] = {
    val dbname: String = "play-test-" + scala.util.Random.nextInt
    Map(
        ("mongodb." + name + ".db" -> dbname),
        ("mongodb." + name + ".port" -> EmbeddedMongoTestPort.toString))
}

override def around[T <% Result](t: => T) = {
    running(FakeApplication(additionalConfiguration = inMemoryMongoDatabase(), additionalPlugins = Seq("se.radley.plugin.salat.SalatPlugin"))) {
        t // execute t inside a http session
    }
}

The FakeApplication uses the default application.conf configuration in the conf directory and additional configuration for the test databases that are created for each test.
This was working find until we setup a mongodb replicat set. Now the application.conf contains configuration for this replicat set

mongodb.default.replicaset {
host1.host = "localhost"
host1.port = 27017
host2.host = "localhost"
host2.port = 27018
host3.host = "localhost"
host3.port = 27019
}

As the FakeApplication uses the default configuration the tests fail because the hosts of the replicaset cannot be found. I want to have a different configuration for my tests, basically remove the mongodb.default.replicaset entry. If mongodb.default.replicaset were a simple Map[String, String] that would be easy as I could just add it to the additonalConfiguration but when I try to do that it fails because the expected value type is not a String but an Object. I have also tried to provide a separate test.conf file to the FakeApplication via the path parameter.

override def around[T <% Result](t: => T) = {
    running(FakeApplication(path = new java.io.File("conf/test.conf"), additionalConfiguration = inMemoryMongoDatabase(), additionalPlugins = Seq("se.radley.plugin.salat.SalatPlugin"))) {
        t // execute t inside a http session
    }
}

That didn't work either as it didn't load any config.

I would greatly appreciate any help. Thanks.

Chris

Answer

tdb picture tdb · Oct 4, 2013

We had a similar problem loading extra configurations for our integration tests. We found populating maps manually to be tedious so we used the following approach:

private Configuration additionalConfigurations;
@Before
public void initialize(){
    Config additionalConfig = ConfigFactory.parseFile(new File("conf/integration.conf"));
    additionalConfigurations = new Configuration(additionalConfig);
}
@Test
public void testPropertiesGetLoaded() throws Exception{
    running(testServer(3333, fakeApplication(additionalConfigurations.asMap())), HTMLUNIT, new Callback<TestBrowser>(){
        public void invoke(TestBrowser browser){
            String specificProperty = Play.application().configuration().getString("specific.property");
            System.out.println(specificProperty);
        }
    });
}

I don't know if there is a nice method on the Scala side of things, we are doing all our code in java.