Skip to content

Instantly share code, notes, and snippets.

@mia-0032
Created May 9, 2013 04:09
Show Gist options
  • Save mia-0032/5545502 to your computer and use it in GitHub Desktop.
Save mia-0032/5545502 to your computer and use it in GitHub Desktop.
PlayFramework2.0でHelloWorld+UnitTest
@(title: String, message: String)
<!DOCTYPE html>
<html>
<head>
<title>@title</title>
</head>
<body>
<p>@message</p>
</body>
</html>
package controllers;
import play.mvc.*;
import views.html.*;
public class HelloWorld extends Controller {
public static Result index() {
return ok("<b>Hello World</b>").as("text/html");
}
public static Result fetchTemplate(){
return ok(hello.render("タイトルは未定", "こんにちは、世界"));
}
public int getNumber(){
return 1;
}
}
import controllers.HelloWorld;
import controllers.routes;
import org.junit.*;
import play.mvc.*;
import play.test.*;
import play.libs.F.*;
import static org.fest.assertions.Assertions.assertThat;
import static play.test.Helpers.*;
import static org.fest.assertions.Assertions.*;
public class HelloWorldTest {
@Test
public void indexメソッドで想定文字列が返ってくる(){
Result result = callAction(
routes.ref.HelloWorld.index()
);
assertThat(status(result)).isEqualTo(OK);
assertThat(contentType(result)).isEqualTo("text/html");
assertThat(contentAsString(result)).contains("<b>Hello World</b>");
}
@Test
public void fetchTemplateメソッドで想定文字列が返ってくる(){
Result result = callAction(
routes.ref.HelloWorld.fetchTemplate()
);
assertThat(status(result)).isEqualTo(OK);
assertThat(contentType(result)).isEqualTo("text/html");
assertThat(contentAsString(result)).contains("<title>タイトルは未定</title>");
assertThat(contentAsString(result)).contains("こんにちは、世界");
}
@Test
public void getNumberメソッドで想定数値が返ってくる(){
HelloWorld hw = new HelloWorld();
int num = hw.getNumber();
assertThat(num).isEqualTo(1);
}
}
GET / controllers.HelloWorld.index()
GET /template controllers.HelloWorld.fetchTemplate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment