Play provides a number of classes and convenience methods that assist with functional testing. Most of these can be found either in the play.test
package or in the Helpers
class.
You can add these methods and classes by importing the following:
Play frequently requires a running Application
as context. To provide an environment for tests, Play provides helpers that produce new application instances for testing:
If you're using Guice for [[dependency injection|JavaDependencyInjection]] then an Application
for testing can be [[built directly|JavaTestingWithGuice]]. You can also inject any members of a test class that you might need. It's generally best practice to inject members only in functional tests and to manually create instances in unit tests.
To run tests with an Application
, you can do the following:
You can also extend WithApplication
, this will automatically ensure that an application is started and stopped for each test method:
To run tests with an Application
[[created by Guice|JavaTestingWithGuice]], you can do the following:
Note that there are different ways to customize the Application
creation when using Guice to test.
Sometimes you want to test the real HTTP stack from within your test. You can do this by starting a test server:
Just as there exists a WithApplication
class, there is also a WithServer
which you can extend to automatically start and stop a TestServer
for your tests:
If you want to test your application from with a Web browser, you can use Selenium WebDriver. Play will start the WebDriver for you, and wrap it in the convenient API provided by FluentLenium.
And, of course there, is the WithBrowser
class to automatically open and close a browser for each test:
Instead of calling the Action
yourself, you can let the Router
do it: