JUnit unit test, test upload photos

Source: Internet
Author: User
Tags file upload int size json
/**
* Upload file Unit Test
*
*/
public class Uploadimgtest extends Abstracttest {

/**
* Convert images to byte[by image Path]
*/
public static byte[] ImageToByteArray (String imgpath) {
Bufferedinputstream in;
try {
in = new Bufferedinputstream (new FileInputStream (Imgpath));
Bytearrayoutputstream out = new Bytearrayoutputstream ();
int size = 0;
byte[] temp = new byte[1024];
while ((size = In.read (temp))!=-1) {
Out.write (temp, 0, size);
}
In.close ();
return Out.tobytearray ();
} catch (IOException e) {
E.printstacktrace ();
return null;
}
}
/**
* Test template file Upload
*/
@Test
public void Testuploadsample () throws exception{

byte[]info=

Final String fileName = "Get.jpg";
Final byte[] content = This.imagetobytearray ("d:/file/get.jpg");

SYSTEM.OUT.PRINTLN (content);
Mockmultipartfile file =
New Mockmultipartfile ("File", FileName, "Image/jpeg", content);//convert byte[] images to the Mockmultipartfile type that the server can go to
Mockmultiparthttpservletrequest request1 = new Mockmultiparthttpservletrequest ();//Impersonation Request
Mockhttpservletresponse response1 = new Mockhttpservletresponse ();//Analog response
String url = "/files/image/uploadphoto.do";//Request Path
Request1.setrequesturi (URL);
Request1.setmethod ("POST");
Request1.addparameter ("Goodsid", "148");
Request1.addfile (file);
Handle (Request1, response1);
String json = response1.getcontentasstring ();
Responseresult sample = (Responseresult) Jsonobject.tobean (Jsonobject.fromobject (JSON),
Responseresult.class);

Assert.assertnotnull (sample);

}



}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.