You should use callAction to use withFormUrlEncodedBody
@Test
public void testMyAction() {
running(fakeApplication(), new Runnable() {
public void run() {
Map<String,String> data = new HashMap<String, Object>();
data.put("param1", "test-1");
data.put("param2", "test-2");
data.put("file", file);
Result result = callAction(
controllers.whatever.action(),
fakeRequest().withFormUrlEncodedBody(data)
)
...
}
}
}
I use only Scala api for Play Framework 2 but I dont think you can test the multipart form using withFormUrlEncodedBody.
You can do in this way in Scala:
import play.api.libs.Files._
import play.api.mvc.MultipartFormData._
class MyTestSpec extends Specification {
"mytest should bla bla bla" in {
running(FakeApplication(aditionalConfiguration = inMemoryDatabase())) {
val data = new MultipartFormData(Map(
("param1" -> Seq("test-1")),
("param2" -> Seq("test-2"))
), List(
FilePart("payload", "message", Some("Content-Type: multipart/form-data"), play.api.libs.Files.TemporaryFile(new java.io.File("/tmp/pepe.txt")))
), List(), List())
val Some(result) = routeAndCall(FakeRequest(POST, "/route/action", FakeHeaders(), data))
...
}
}
}
I guess you can translate it to Java, I don't know how to code it in Java sorry.
P.D: Sorry for my English I'm still learning
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…