Test Configuration Settings in Play Framework
A common need when writing integration or unit tests is to have a different configuration than what would be used for production. Play Framework provides the FakeApplication class to assist with test configurations. It acts mostly the same as the normal Application class. When instantiating a new FakeApplication, we can pass in various values that we can overwrite. The two important aspects we can overwrite are:
- GlobalSettings
- Values from application.conf
Configuring Tests
For my examples, I will be using the ScalaTest + Play library. Add the following line to your build.sbt file if you want to use the library as well:
"org.scalatestplus" %% "play" % "1.1.0" % "test"
Here is an example of a suite mixin that can be used to overwrite the GlobalSettings. It is based off of the OneAppPerSuite mixin from ScalaTest + Play library:
This is how you can overwrite specific conf values (also based on the OneAppPerSuite mixin)
Before and After a Test
Executing a task before and/or after a test runs is a very common thing to do in testing. You can use the BeforeAndAfter mixin to execute code before and after each test.
Putting it all together
Additional Information
Be sure to read ScalaTest + Play documentation as well as Play Framework testing documentation.