Build and run the android unit test environment

Source: Internet
Author: User

Business Layer Code:

Package CN. BZU. fileoperation; import Java. io. bytearrayoutputstream; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import android. content. context; public class fileservice {private context; Public fileservice (context) {super (); this. context = context;} // Save Public void save (string filename, string content) throws exception {fileoutputstream Fos = Co Ntext. openfileoutput (filename, context. mode_private); FOS. write (content. getbytes (); FOS. close () ;}// read data Public String readfile (string filename) throws ioexception {fileinputstream FCM = context. openfileinput (filename); int Len = 0; byte [] buffer = new byte [1024]; bytearrayoutputstream baos = new bytearrayoutputstream (); // output data to the memory while (LEN = Fi. read (buffer ))! =-1) {// if the data volume is large, the 2nd read data may overwrite the 1st read data to baos. write (buffer, 0, Len);} byte [] DATA = baos. tobytearray (); // obtain the baos in which data is stored in binary format. close (); FCM. close (); return new string (data); // convert binary data to the corresponding string }}

Unit test environment setup:

Add the code under <Application> </Application> In androidmanifest. xml:

 <uses-library android:name="android.test.runner" />

Add the code outside <Application> </Application>:

 <instrumentation        android:name="android.test.InstrumentationTestRunner"        android:targetPackage="cn.bzu.fileoperation" >    </instrumentation>

Business Layer Test code:

Package CN. BZU. fileoperation; import Java. io. ioexception; import android. test. androidtestcase; public class fileservicetest extends androidtestcase {public void testsavefile () {fileservice = new fileservice (getcontext (); try {fileservice. save ("file2.txt", "");} catch (exception e) {// todo auto-generated catch blocke. printstacktrace () ;}} public void testreadfile () {fileservice = new fileservice (getcontext (); try {string S = fileservice. readfile ("file2.txt"); system. out. print (s);} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}

Right-click the test class -- run as -- Android JUnit test;

If no error occurs, you can run it in the virtual machine.

 

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.