使用自定义application.conf测试播放控制器

时间:2016-05-31 05:06:27

标签: testing playframework

我尝试在play框架中进行简单的控制器测试

public class AuthTest extends WithServer {

@Test
public void authByLoginErrorForm() {
    Map<String,String> data = new HashMap<>();
    data.put("username", "");
    data.put("password", "");

    Result result = routeAndCall(fakeRequest("POST", "/auth_by_login"), Helpers.DEFAULT_TIMEOUT);
    assertEquals(400, result.status());
}
}

builds.sbt我为测试模式设置自定义配置。他需要在测试中排除创建数据库池连接。

javaOptions in Test ++= Seq(
"-Dlogger.file=test/conf/logback.xml",
"-Xms512M",
"-Xmx1536M",
"-XX:MaxPermSize=384M",
"-XX:MaxMetaspaceSize=384M",
"-Dconfig.file=test/conf/application.conf"
)

测试运行后我收到错误:

[error] Test controller.AuthTest.authByLoginErrorForm failed: java.lang.RuntimeException: java.lang.ClassNotFoundException: Routes, took 2.881 sec
[error]     at play.test.Helpers.routeAndCall(Helpers.java:293)
[error]     at controller.AuthTest.authByLoginErrorForm(AuthTest.java:28)
[error]     ...
[error] Caused by: java.lang.ClassNotFoundException: Routes
[error]     at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[error]     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[error]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
[error]     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[error]     at play.test.Helpers.routeAndCall(Helpers.java:289)
[error]     ... 37 more
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error]     controller.AuthTest

我将routes文件复制到test.conf目录(附近)application.conf,但是出现了同样的错误。

1 个答案:

答案 0 :(得分:0)

刚刚将测试更改为此代码,一切正常。

@Test
public void authByLoginErrorForm() {

    Map<String,String> data = new HashMap<>();
    data.put("username", "");
    data.put("password", "");

    Http.RequestBuilder request = new Http.RequestBuilder()
            .method("POST")
            .uri("/auth_by_login").bodyForm(data);

    Result result = route(request, Helpers.DEFAULT_TIMEOUT);
    assertEquals(200, result.status());
}