#测试应用程序
测试资源文件必须放在应用的test目录下。你可以通过play控制台使用test和test-only来执行测试任务。
1.使用JUnit
Play2.0默认使用JUnit进行测试。
package test;
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);
}
...
2.在伪应用程序中运行
如果你需要测试的代码依赖一个运行中的程序,你可以在fly上轻松的创建一个FakeApplication。
@Test
public void findById() {
running(fakeApplication(), new Runnable() {
public void run() {
Computer macintosh = Computer.find.byId(21l);
assertThat(macintosh.name).isEqualTo("Macintosh");
assertThat(formatted(macintosh.introduced)).isEqualTo("1984-01-24");
}
});
}
你也快传递(或重写)另外的应用程序配置,或者mock任何插件。例如使用默认的内存数据库创建一个FakeApplication。
fakeApplication(inMemoryDatabase())
#编写功能测试
1.测试模板
因为模板是个标准的Scala函数,你可以在test中执行它并检查结果。
@Test
public void renderTemplate() {
Content html = views.html.index.render("Coco");
assertThat(contentType(html)).isEqualTo("text/html");
assertThat(contentAsString(html)).contains("Coco");
}
2.测试你的Controllers
通过检索路由,你可以得到一个action的引用,如controllers.routes.ref.Application.index。然后你可以调用它:
@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");
}
3.测试路由
不同于自己调用action,你可以让路由这么做:
@Test
public void badRoute() {
Result result = routeAndCall(fakeRequest(GET, "/xx/Kiki"));
assertThat(result).isNull();
}
4.启动真的Http服务器
有时候你想测试真的Http栈模型。那么你可以启动一个测试服务器:
@Test
public void testInServer() {
running(testServer(3333), new Callback0() {
public void invoke() {
assertThat(
WS.url("http://localhost:3333").get().get().status
).isEqualTo(OK);
}
});
}
4.在web浏览器中测试
如果你想在web浏览器中测试你的应用,你可以使用selenum WebDriver。Play会为你启动WebDriver,并且
用FluentLenium提供的简便API包装它。
@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");
}
});
}
分享到:
相关推荐
NULL 博文链接:https://shenbai.iteye.com/blog/1522718
Play framework 2.0入门教程(三)的源代码,Play留言板,详见http://blog.csdn.net/wfdztl521/article/details/8545838
Play framework 2.0入门教程(四)的源代码,Play留言板(数据库版),详见http://blog.csdn.net/wfdztl521/article/details/8766988
将Elasticsearch集成到您的Play... $ git clone https://github.com/eduardofcbg/playframework2-elasticsearch.git$ cd playframework2-elasticsearch$ sbt publish-local 在您的项目上,添加以下依赖项声明: ...
更新播放 2.4.0添加测试规范Playframework 2.2.1 (Scala) 和 MongoDb (Salat) 的第一步使用 MongoDb 而不是 Anorm 的待办事项应用程序我们将为 MongoDb 使用 Salat 库( )。 Play 2 有一个替代的“mongo-jackson-...
Playframework 2.3.1 (Java) 和 MongoDb (Jackson) 的第一步 不要忘记启动mongodb守护进程! 它应该使用默认主机 (localhost) 和默认端口。 运行应用程序: sbt run 如果运行本教程有任何问题,请报告,谢谢!
play-services-base.properties
google-play-services版本15.0.1,依赖工程可直接引用到项目中,适用Eclipse及AndroidStudio开发环境,快捷方便,本人亲自整理,后续资源有问题可咨询。若后续google-play-services如需其他版本迭代,也可让我更进。
解决: Could not find play-services-basement.aar (com.google.android.gms:play-services-basement:15.0.1). Searched in the following locations: ...
TLeague_ A Framework for Competitive Self-Play basedDistributed Multi-Agent RL
使用Play Framework 2.0 +,jQuery,Backbone.js,Marionette.js和Postgresql用Java / Scala编写的ERP应用程序 开发人员虚拟机 您可以下载此zip文件,其中包含安装并配置了Ubuntu 16.04,PostgreSQL,PlayFramework...
play2.0 for java developers.pdf
框架id(framework ID) - 141 - 从命令行设置框架id - 142 - 22.生产部署 - 142 - application.conf - 142 - 设置框架为prod模式: - 142 - 定义一个真实的数据库: - 143 - 禁止JPA的自动结构更新: - 143 - 定义一...
苹果carplay开发插件CarPlay Communication Plug-in,官方下载,
play2-war-plugin, 用于 Play Framework 2.x的WAR插件 用于 Play Framework 2.x的 WAR插件Current versions: Play 2.2.x : 1.2.1 Play 2.3.0 -> 2.3.1 : 1.3-beta1 (Scala 2.1
CarPlay Communication Plug-in
play-services-ads-lite-18.3.0是2019-11-23为止,最新的安卓admob sdk,用于原生安卓介入 加入工程后: import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import ...
AWS Elastic Beanstalk 上的 Nginx + Playframework 最新的公共 AMI: ami-c0ae98a8 2015 年 4 月 1 日构建新自定义 AMI 的说明首先,在 Elastic Beanstalk 上创建一个 tomcat 应用程序。 然后通过 SSH 连接到创建的...
对学习java开发框架及搜索的同学很有用,欢迎下载!
play service ads 单独模块,用于谷歌广告开发,aar 文件需要使用 Android Studio 作为编译工具使用,eclipse 开发者无须下载。