Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manual/working/javaGuide/main/tests/JavaFunctionalTest.md #1251

Open
garbagetown opened this issue Feb 8, 2016 · 0 comments
Open

manual/working/javaGuide/main/tests/JavaFunctionalTest.md #1251

garbagetown opened this issue Feb 8, 2016 · 0 comments
Milestone

Comments

@garbagetown
Copy link
Member

--- /Users/garbagetown/Desktop/2.2.0/manual/working/javaGuide/main/tests/JavaFunctionalTest.md  2016-02-07 23:19:03.000000000 +0900
+++ //Users/garbagetown/Desktop/2.4.x/manual/working/javaGuide/main/tests/JavaFunctionalTest.md 2016-02-07 23:19:30.000000000 +0900
@@ -1,81 +1,73 @@
+<!--- Copyright (C) 2009-2015 Typesafe Inc. <http://www.typesafe.com> -->
 # Writing functional tests

-## Testing a template
+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`](api/java/play/test/package-summary.html) package or in the [`Helpers`](api/java/play/test/Helpers.html) class.

-As a template is a standard Scala function, you can execute it from a test and check the result:
+You can add these methods and classes by importing the following:

-'''
-@Test
-public void renderTemplate() {
-  Content html = views.html.index.render("Coco");
-  assertThat(contentType(html)).isEqualTo("text/html");
-  assertThat(contentAsString(html)).contains("Coco");
-}
+'''java
+import play.test.*;
+import static play.test.Helpers.*;
 '''

-You can find the complete list of the *test helpers* in the [Helper class API documentation](http://www.playframework.com/documentation/api/2.1.1/java/play/test/Helpers.html). 
+## FakeApplication

-## Testing your controllers
+Play frequently requires a running [`Application`](api/java/play/Application.html) as context: it is usually provided from [`play.Play.application()`](api/java/play/Play.html).

-You can also retrieve an action reference from the reverse router, such as `controllers.routes.ref.Application.index`. You can then invoke it:
+To provide an environment for tests, Play provides a [`FakeApplication`](api/java/play/test/FakeApplication.html) class which can be configured with a different Global object, additional configuration, or even additional plugins.

-'''
-@Test
-public void callIndex() {
-    Result result = callAction(
-      controllers.routes.ref.Application.index("Kiki")
-    );
-    assertThat(status(result)).isEqualTo(OK);
-    assertThat(contentType(result)).isEqualTo("text/html");
-    assertThat(charset(result)).isEqualTo("utf-8");
-    assertThat(contentAsString(result)).contains("Hello Kiki");
-}
-'''
+@[test-fakeapp](code/javaguide/tests/FakeApplicationTest.java)

-## Testing the router
+## Injecting tests

-Instead of calling the `Action` yourself, you can let the `Router` do it:
+If you're using Guice for [[dependency injection|JavaDependencyInjection]] then an `Application` for testing can be [[built directly|JavaTestingWithGuice]], instead of using FakeApplication. 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
-public void badRoute() {
-  Result result = routeAndCall(fakeRequest(GET, "/xx/Kiki"));
-  assertThat(result).isNull();
-}
-'''
+@[test-injection](code/javaguide/tests/InjectionTest.java)

-## Starting a real HTTP server
+## Testing with an application

-Sometimes you want to test the real HTTP stack from with your test. You can do this by starting a test server:
+To run tests with an `Application`, you can do the following:

-'''
-@Test
-public void testInServer() {
-  running(testServer(3333), new Runnable() {
-      public void run() {
-         assertThat(
-           WS.url("http://localhost:3333").get().get().getStatus()
-         ).isEqualTo(OK);
-      }
-  });
-}
-'''
+@[test-running-fakeapp](code/javaguide/tests/FakeApplicationTest.java)

-## Testing from within a web browser
+You can also extend [`WithApplication`](api/java/play/test/WithApplication.html), this will automatically ensure that an application is started and stopped for you:

-If you want to test your application from with a Web browser, you can use [Selenium WebDriver](http://code.google.com/p/selenium/?redir=1). Play will start the WebDriver for your, and wrap it in the convenient API provided by [FluentLenium](https://github.com/FluentLenium/FluentLenium).
+@[test-withapp](code/javaguide/tests/FunctionalTest.java)

-'''
-@Test
-public void runInBrowser() {
-    running(testServer(3333), HTMLUNIT, new Callback<TestBrowser>() {
-        public void invoke(TestBrowser browser) {
-           browser.goTo("http://localhost:3333"); 
-           assertThat(browser.$("#title").getTexts().get(0)).isEqualTo("Hello Guest");
-           browser.$("a").click();
-           assertThat(browser.url()).isEqualTo("http://localhost:3333/Coco");
-           assertThat(browser.$("#title", 0).getText()).isEqualTo("Hello Coco");
-        }
-    });
-}
-'''
+This will ensure that a [`FakeApplication`](api/java/play/test/FakeApplication.html) will be started and stopped for each test method.
+
+## Testing with a Guice application
+
+To run tests with an `Application` [[created by Guice|JavaTestingWithGuice]], you can do the following:
+
+@[test-guiceapp](code/tests/guice/JavaGuiceApplicationBuilderTest.java)
+
+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](code/javaguide/tests/FunctionalTest.java)
+
+Just as there exists a `WithApplication` class, there is also a [`WithServer`](api/java/play/test/WithServer.html) which you can extend to automatically start and stop a [`TestServer`](api/java/play/test/TestServer.html) for your tests:
+
+@[test-withserver](code/javaguide/tests/ServerFunctionalTest.java)
+
+## Testing with a browser
+
+If you want to test your application from with a Web browser, you can use [Selenium WebDriver](https://github.com/seleniumhq/selenium). Play will start the WebDriver for you, and wrap it in the convenient API provided by [FluentLenium](https://github.com/FluentLenium/FluentLenium).
+
+@[test-browser](code/javaguide/tests/FunctionalTest.java)
+
+And, of course there, is the [`WithBrowser`](api/java/play/test/WithBrowser.html) class to automatically open and close a browser for each test:
+
+@[test-withbrowser](code/javaguide/tests/BrowserFunctionalTest.java)
+
+## Testing the router
+
+Instead of calling the `Action` yourself, you can let the `Router` do it:
+
+@[bad-route-import](code/javaguide/tests/FunctionalTest.java)
+
+@[bad-route](code/javaguide/tests/FunctionalTest.java)

@garbagetown garbagetown added this to the 2.4.x milestone Feb 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants