Use JUnit for testing in Android

Source: Internet
Author: User

In java Development, it is common to use junit for unit testing. What about android? The answer is yes!

The usage is also very simple. You only need to add two lines of configuration in AndroidManifest. xml, and then write a class that inherits AndroidTestCase. The other is the same as using junit in java.

Sample AndroidManifest. xml code:

 
 
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2. <Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
  3. Package = "com. javake. hzy. filesave"
  4. Android: versionCode = "1"
  5. Android: versionName = "1.0" type = "codeph" text = "/codeph">
  6. <Uses-sdk android: minSdkVersion = "8"/>
  7. <! -- Junit test configuration key configuration first -->
  8. <Instrumentation android: name = "android. test. InstrumentationTestRunner"
  9. Android: targetPackage = "com. javake. hzy. filesave" android: label = "my app test"/>
  10. <Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
  11. <! -- The second key configuration of junit test configuration -->
  12. <Uses-library android: name = "android. test. runner"/>
  13. <Activity android: name = ". FileSave"
  14. Android: label = "@ string/app_name">
  15. <Intent-filter>
  16. <Action android: name = "android. intent. action. MAIN"/>
  17. <Category android: name = "android. intent. category. LAUNCHER"/>
  18. </Intent-filter>
  19. </Activity>
  20.  
  21. </Application>
  22. </Manifest>

Sample Code for unit test:

 
 
  1. Package com. javake. hzy. filesave;
  2. Import java. io. FileOutputStream;
  3. Import android. content. Context;
  4. Import android. test. AndroidTestCase;
  5.  
  6. /**
  7. * The test class is used to test file read/write operations.
  8. * For unit tests, you only need to inherit the AndroidTestCase class.
  9. * Use test as the prefix before testing.
  10. * Right-click the Test and choose Android JUnit Test from the run as pop-up menu.
  11. * @ Author hzy
  12. *
  13. */
  14. Public class MyTest extends AndroidTestCase {
  15. /**
  16. * Test Method 1: create a file and write a string
  17. */
  18. Public void test01 (){
  19. Context context = this. getContext ();
  20. System. out. println (context );
  21. Try {
  22. FileOutputStream out = context. openFileOutput ("hello.txt", Context. MODE_PRIVATE );
  23. Out. write ("hello world my name is hzy". getBytes ());
  24. Out. close ();
  25. } Catch (Exception e ){
  26. E. printStackTrace ();
  27. }
  28. }
  29. }

Related Article

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.