2

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

4

2 回答 2

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");

            }
        });
    }

}
于 2012-08-03T14:18:57.923 回答
-1

play.test.FunctionalTest 不存在。

如何编写函数测试应该阅读这里的文档。

测试您的应用程序

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

使用 JUnit

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

``` 封装测试;

导入 org.junit.*;

导入play.mvc。; 导入play.test。; 导入 play.libs.F.*;

导入静态 play.test.Helpers。; 导入静态 org.fest.assertions.Assertions。;

公共类 SimpleTest {

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

}```

于 2012-05-08T10:44:59.827 回答