没有play.test.FunctionalTest在play 2.0中如何编写功能测试?

时间:2012-05-08 09:43:42

标签: java playframework-2.0

我是Java&的新手玩框架。目前我正在使用Play framework 2.0,并且没有类play.test.FunctionalTest。如何编写功能测试?

2 个答案:

答案 0 :(得分:1)

请参阅示例应用程序中的示例。

其中一个现成的例子是$ {PLAY_FRAMEWORK} /samples/java/computer-database/test/FunctionalTest.java,引用如下:

import org.junit.*;

import java.util.*;

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

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

public class FunctionalTest {

    @Test
    public void redirectHomePage() {
        running(fakeApplication(), new Runnable() {
           public void run() {
               Result result = callAction(controllers.routes.ref.Application.index());

               assertThat(status(result)).isEqualTo(SEE_OTHER);
               assertThat(redirectLocation(result)).isEqualTo("/computers");
           }
        });
    }

    @Test
    public void listComputersOnTheFirstPage() {
        running(fakeApplication(), new Runnable() {
           public void run() {
               Result result = callAction(controllers.routes.ref.Application.list(0, "name", "asc", ""));

               assertThat(status(result)).isEqualTo(OK);
               assertThat(contentAsString(result)).contains("574 computers found");
           }
        });
    }

    @Test
    public void filterComputerByName() {
        running(fakeApplication(), new Runnable() {
           public void run() {
               Result result = callAction(controllers.routes.ref.Application.list(0, "name", "asc", "Apple"));

               assertThat(status(result)).isEqualTo(OK);
               assertThat(contentAsString(result)).contains("13 computers found");
           }
        });
    }

    @Test
    public void createANewComputer() {
        running(fakeApplication(), new Runnable() {
            public void run() {
                Result result = callAction(controllers.routes.ref.Application.save());

                assertThat(status(result)).isEqualTo(BAD_REQUEST);

                Map<String,String> data = new HashMap<String,String>();
                data.put("name", "FooBar");
                data.put("introduced", "badbadbad");
                data.put("company.id", "1");

                result = callAction(
                    controllers.routes.ref.Application.save(), 
                    fakeRequest().withFormUrlEncodedBody(data)
                );

                assertThat(status(result)).isEqualTo(BAD_REQUEST);
                assertThat(contentAsString(result)).contains("<option value=\"1\" selected>Apple Inc.</option>");
                assertThat(contentAsString(result)).contains("<input type=\"text\" id=\"introduced\" name=\"introduced\" value=\"badbadbad\" >");
                assertThat(contentAsString(result)).contains("<input type=\"text\" id=\"name\" name=\"name\" value=\"FooBar\" >");

                data.put("introduced", "2011-12-24");

                result = callAction(
                    controllers.routes.ref.Application.save(), 
                    fakeRequest().withFormUrlEncodedBody(data)
                );

                assertThat(status(result)).isEqualTo(SEE_OTHER);
                assertThat(redirectLocation(result)).isEqualTo("/computers");
                assertThat(flash(result).get("success")).isEqualTo("Computer FooBar has been created");

                result = callAction(controllers.routes.ref.Application.list(0, "name", "asc", "FooBar"));
                assertThat(status(result)).isEqualTo(OK);
                assertThat(contentAsString(result)).contains("One computer found");

            }
        });
    }

}

答案 1 :(得分:-1)

play.test.FunctionalTest不存在。

如何编写功能测试应该在这里阅读文档。

测试您的应用程序

测试源文件必须放在应用程序的test文件夹中。您可以使用testtest-only任务从Play控制台运行测试。

使用JUnit

测试Play 2应用程序的默认方式是[[JUnit | http://www.junit.org/]。

``` 包装测试;

import org.junit。*;

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

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

public class SimpleTest {

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

} ```