[JAVA] Play Framework studying test

(Reference) Books you are studying https://www.amazon.co.jp/Play-Framework-2%E5%BE%B9%E5%BA%95%E5%85%A5%E9%96%80-Java%E3%81%A7%E3%81%AF%E3%81%98%E3%82%81%E3%82%8B%E3%82%A2%E3%82%B8%E3%83%A3%E3%82%A4%E3%83%ABWeb%E9%96%8B%E7%99%BA-%E6%B4%A5%E8%80%B6%E4%B9%83/dp/4798133922/ref=cm_cr_srp_d_product_top?ie=UTF8

About the test

In the play framework, tests using a test library called JUnit are prepared as standard. スクリーンショット 2018-03-20 20.41.41.png

・ApplicationTest.java → Tests for application behavior

・IntegrationTest.java → A number of small tests that are integrated

ApplicationTest.java code

import com.fasterxml.jackson.databind.JsonNode;
import org.junit.*;

import play.mvc.*;
import play.test.*;
import play.data.DynamicForm;
import play.data.validation.ValidationError;
import play.data.validation.Constraints.RequiredValidator;
import play.i18n.Lang;
import play.libs.F;
import play.libs.F.*;

import static play.test.Helpers.*;
import static org.fest.assertions.Assertions.*;


/**
*
* Simple (JUnit) tests that can call all parts of a play app.
* If you are interested in mocking a whole application, see the wiki for more details.
*
*/
public class ApplicationTest {

    @Test
    public void simpleCheck() {
        int a = 1 + 1;
        assertThat(a).isEqualTo(2);
    }

    @Test
    public void renderTemplate() {
        Content html = views.html.index.render("Your new application is ready.");
        assertThat(contentType(html)).isEqualTo("text/html");
        assertThat(contentAsString(html)).contains("Your new application is ready.");
    }
}

Minimum required package

There are multiple packages that are imported, but the minimum required packages are as follows.

import org.junit.*;
import play.mvc.*;
import static play.test.Helpers.*;
import static org.fest.assertions.Assertions.*;

About the contents

simpleCheck method

The code confirms that 1 + 1 is 2, but that's not the goal, ** define a method here and use assertThat to check the value! ** means. See the following article for the operation of assertThat (a) .isEqualTo (2). (Reference) https://qiita.com/naotawool/items/6512ecbe2fd006dacfd2

 @Test
    public void simpleCheck() {
        int a = 1 + 1;
        assertThat(a).isEqualTo(2);
    }

simpleCheck method

Checking the rendered value.

@Test
    public void renderTemplate() {
        Content html = views.html.index.render("Your new application is ready.");//①
        assertThat(contentType(html)).isEqualTo("text/html");//②
        assertThat(contentAsString(html)).contains("Your new application is ready.");//③
    }

・ ContentType → Returns the content type as a String

・ ContentAsString → Extract the text of the content from Content and return it as a String

・ IsEqualTo ("text / html") → Check if it is text / html. What is text / html? (Reference) http://wa3.i-3-i.info/word15789.html

The processing flow is

① Rendering ↓ ② Check if the text type is html ↓ ③ Check the contents

Code for IntegrationTest.java

import org.junit.*;

import play.mvc.*;
import play.test.*;
import play.libs.F.*;

import static play.test.Helpers.*;
import static org.fest.assertions.Assertions.*;

import static org.fluentlenium.core.filter.FilterConstructor.*;

public class IntegrationTest {

    /**
     * add your integration test here
     * in this example we just check if the welcome page is being shown
     */
    @Test
    public void test() {
        running(testServer(3333, fakeApplication(inMemoryDatabase())), HTMLUNIT, new Callback<TestBrowser>() {
            public void invoke(TestBrowser browser) {
                browser.goTo("http://localhost:3333");//①
                assertThat(browser.pageSource()).contains("Your new application is ready.");//②
            }
        });
    }

}

About the contents

Write the test process in the method called invoke in the test method! The statement being executed is

running(testServer(・ ・ ・),HTMLUNIT,new Callback(){・ ・ ・}) 

The argument of the running method is · TestServer instance ・ Specifying WebDriver -Callback instance for callback that performs post-execution processing I understand that this is something like this.

public void invoke(TestBrower browser){}

① Access the URL of the argument with the goTo method (2) Get the page source with browser.pageSource (). This will compare it to the string displayed in TestBrowser browser

Recommended Posts

Play Framework studying test
Play Framework study
Play Framework studying value passing, form helper
Play Framework2.5 (Java) Tips
play framework personal notes
Play Framework Study Memo Database ①
First Play Framework personal notes
Introduced Dozer to Play Framework
Validation function in Play Framework
test
Play Framework Study Memo Database ②Read
Play Framework 2.6 (Java) development environment creation
Play Framework Study Momo DB Update
Double submit measures with Play Framework
test
test
test
Test Spring framework controller with Junit
Play framework scheduled jobs and crontab
How to install Play Framework 2.6 for Mac
Authentication function in Play Framework [Access restrictions]
How to use BootStrap with Play Framework
Play Framework 2.6 (Java) environment construction in Eclipse
Authentication function with Play Framework [Registration and authentication]
Post to Slack from Play Framework 2.8 (Java)