scala - Unit testing file upload in a controller with Java Play Framework 2.3.x -


after working of day feel close solution on how test controller method accepts file uploads junit. juint test code follows:

map<string, string> postdata = makepostmap(uploadform); file file = new file("test/resources/shared/uploads/blank.csv"); temporaryfile temporaryfile = new temporaryfile(file);  multipartformdata.filepart filepath = new multipartformdata.filepart(         "file",         "file.csv",         new scala.some<>("text/csv"),         temporaryfile);  list<multipartformdata.filepart> fileparts = lists.newarraylist(filepath); scala.collection.immutable.seq files = javaconversions.asscalabuffer(fileparts).tolist();  map<string, scala.collection.immutable.seq<string>> postdata2 = new hashmap<>(); (string s : postdata.keyset()) {     postdata2.put(s, javaconversions.asscalabuffer(lists.newarraylist(postdata.get(s))).tolist()); } scala.collection.immutable.map<string, scala.collection.immutable.seq<string>> scalamap =         javaconversions.mapasscalamap(postdata2).tomap(predef.<tuple2<string, scala.collection.immutable.seq<string>>>conforms());  multipartformdata formdata = new multipartformdata(scalamap, files, null, null); anycontentasmultipartformdata body = new anycontentasmultipartformdata(formdata);  // run login(employee); string url = routes.managecontacts.uploadcsv().url(); fakerequest fakerequest = new fakerequest(post, url).withbody(body); fakerequest = getauthenticatedrequest(fakerequest, employee);  result = route(fakerequest);  assertthat(status(result)).isequalto(ok) 

however, exception (below) when fakerequest routed to.

[error] test controllers.managecontactstest.testuploadcsv failed: scala.matcherror: anycontentasmultipartformdata(multipartformdata(map(clearexisting -> list(false), survey -> list(11), bosmode -> list(false)),list(filepart(file,file.csv,some(text/csv),temporaryfile(test/resources/shared/uploads/blank.csv))),null,null)) (of class play.api.mvc.anycontentasmultipartformdata), took 0.255 sec [error]     @ play.api.test.routeinvokers$class.jroute(helpers.scala:255) [error]     @ play.api.test.helpers$.jroute(helpers.scala:403) [error]     @ play.api.test.helpers.jroute(helpers.scala) [error]     @ play.test.helpers.route(helpers.java:445) [error]     @ play.test.helpers.route(helpers.java:437) [error]     @ play.test.helpers.route(helpers.java:433) [error]     @ controllers.managecontactstest.testuploadcsv(managecontactstest.java:121) [error]     ... 

diving down stack trace, find following scala match statement in file: /users/jcreason/bin/playframework-2.3.8/framework/src/play-test/src/main/scala/play/api/test/helpers.scala:253

  def jroute[t](app: application, r: fakerequest[t]): option[future[result]] = {     (r.body: @unchecked) match {       case body: anycontentasformurlencoded => route(app, r, body)       case body: anycontentasjson => route(app, r, body)       case body: anycontentasxml => route(app, r, body)       case body: anycontentastext => route(app, r, body)       case body: anycontentasraw => route(app, r, body)       case body: anycontentasempty.type => route(app, r, body)       //case _ => matcherror thrown     }   } 

since i'm passing through anycontentasmultipartformdata, throws exception it's not handled match. know how around this? or point me in direction of different solution (aside obvious answers selenium)?

for reference, pulled of code from:

http://www.erol.si/2014/02/how-to-test-file-uploads-in-play-framework-java/

this code may useful:

import static play.test.helpers.*; import static org.junit.assert.*; import java.util.*; import org.junit.test; import akka.stream.javadsl.source; import akka.util.bytestring; import play.mvc.http.multipartformdata.*; import play.mvc.http.requestbuilder; import play.mvc.result; import play.test.*;  class uploadfiletest extends withapplication {     @test     public void uploadtest() {         part<source<bytestring, ?>> part = new filepart<>("key", "filename", "application/octet-stream",                 source.empty());         list<part<source<bytestring, ?>>> data = arrays.aslist(part);          requestbuilder requestbuilder = fakerequest(controllers.routes.uploadcontroller.upload()).method(post)                 .bodymultipart(data, mat);          result result = route(app, requestbuilder);          assertequals(helpers.ok, result.status());     } } 

if want send not empty file,
instead of source.empty() use fileio.fromfile(new file("path/to/file"));


Comments