Skip to content

Latest commit

 

History

History
66 lines (34 loc) · 3.2 KB

JavaFunctionalTest.md

File metadata and controls

66 lines (34 loc) · 3.2 KB

Writing functional tests

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:

@test-imports

Creating Application instances for testing

Play frequently requires a running Application as context. To provide an environment for tests, Play provides helpers that produce new application instances for testing:

@test-fakeapp

Injecting tests

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.

@test-injection

Testing with an application

To run tests with an Application, you can do the following:

@test-running-fakeapp

You can also extend WithApplication, this will automatically ensure that an application is started and stopped for each test method:

@test-withapp

Testing with a Guice application

To run tests with an Application [[created by Guice|JavaTestingWithGuice]], you can do the following:

@test-guiceapp

Note that there are different ways to customize the Application creation when using Guice to test.

Testing with a server

Sometimes you want to test the real HTTP stack from within your test. You can do this by starting a test server:

@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:

@test-withserver

Testing with a browser

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.

@test-browser

And, of course there, is the WithBrowser class to automatically open and close a browser for each test:

@test-withbrowser

Testing the router

Instead of calling the Action yourself, you can let the Router do it:

@bad-route-import

@bad-route