Java 将文件上载为FormDataParam进行测试
因此,我在测试我的应用程序时遇到了问题。我正在尝试做REST/HTTP测试。这是我的密码:Java 将文件上载为FormDataParam进行测试,java,rest,http,testing,junit,Java,Rest,Http,Testing,Junit,因此,我在测试我的应用程序时遇到了问题。我正在尝试做REST/HTTP测试。这是我的密码: @Path("/ftpaction") public class JerseyFileUpload { @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) public Response postMsg(@HeaderParam("FTP-Host") String Host, @HeaderPar
@Path("/ftpaction")
public class JerseyFileUpload {
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response postMsg(@HeaderParam("FTP-Host") String Host, @HeaderParam("FTP-Port") String Port,
@HeaderParam("FTP-User") String User, @HeaderParam("FTP-Password") String Password,
@HeaderParam("FTP-Path") String Path, @FormDataParam("file") InputStream inputStream) {
try {
InformationHandler informationHandler = new InformationHandler(Path, Host, Port, User, Password);
CountriesStructure worker = new CountriesStructure();
worker.prepareCountriesStructure(inputStream, true, informationHandler);
} catch (UsernameOrPasswordException e) {
return Response.status(401).entity("Status 401.").build();
} catch (SocketException e) {
return Response.status(404).entity("Status 404.").build();
} catch (IOException e) {
return Response.status(400).entity("Status 400.").build();
} catch (JAXBException e) {
return Response.status(500).entity("Status 500.").build();
} catch (Exception e) {
return Response.status(500).entity("Status 500.").build();
}
return Response.status(200).entity("Success!").build();
}
}
还有我的测试:
@RunWith(HttpJUnitRunner.class)
public class TestMain extends TestCase {
@Rule
public Destination destination = new Destination(this, "http://localhost:8080");
@Context
private Response response;
@HttpTest(method = Method.POST, path = "/JerseyWebApp/ftpaction/upload", content = "{}", file = "/CountriesList.txt", type = MediaType.MULTIPART_FORM_DATA, headers = {
@Header(name = "FTP-Host", value = "localhost"), @Header(name = "FTP-Port", value = "21"),
@Header(name = "FTP-User", value = "ftptest"), @Header(name = "FTP-Password", value = "test"),
@Header(name = "FTP-Path", value = "/test123"), @Header(name = "Accept-Encoding", value = "multipart/form-data")})
public void checkPost() {
System.out.println(response.getBody());
}
}
我无法通过测试读取文件。我不知道我必须做什么,因为我正在使用文件作为“FormDataParam”。有人知道我如何上传文件作为FormDataParam进行测试吗?因为现在它看不到我的文件,只返回“状态400”