In-depth analysis of Android unit test problems

Source: Internet
Author: User

When many people come into contact with the Android unit test, the first response is whether the Android unit test has been fully integrated with JUnit. Unfortunately, this is not a fact. If you follow the running method of JUnit but use JDK instead, you will get an exception.

In fact, the TestCase Class is used to act as the base Class of all unique TestCase in Android. It is a Abstract Class. The inheritance diagram of the Android unit test class is as follows:

 
 
  1. #  
  2. # An unexpected error has been detected by Java Runtime Environment:   
  3. #  
  4. # Internal Error (classFileParser.cpp:2924), pid=4900, tid=4476   
  5. #Error: ShouldNotReachHere()   
  6. #   
  7. # Java VM: Java HotSpot(TM) Client VM (10.0-b19 mixed mode windows-x86)   
  8. # An error report file with more information is saved as:   
  9. # E:\Mydoc\EclipseWorkspace\TestAndroid\hs_err_pid4900.log   
  10. #   
  11. # If you would like to submit a bug report, please visit:   
  12. # http://java.sun.com/webapps/bugreport/crash.jsp   
  13. #  

There are so many XXXTestCase mainly to simplify the work. For example, to test a database access function, you must first start and initialize the database on your own. It is similar here, if you want to test an Activity.

Start it first. ActivityTestCase will automatically help you complete these tasks. ActivityUnitTestCase will pay more attention to the independence of testing, which will minimize the connection between testing and Android unit testing. You can view related Javadoc for other classes and select them as needed. To write a test, find the appropriate XXXTestCase as the base class to inherit and write your own test method.

Obviously, the simplest way to write a test is to inherit the Android unit test and write your own TestCase. Then, write an Activity interface for a group of TestCase, which controls the startup, running, and result report of TestCase.

However, you will soon find out, why do you want to write an interface for the test? This is weird. In this case, a technology is required. It can use the command line Shell to start a group of tests and give the results through the command line. This is the so-called Instrumentation.

To perform unit tests in Java, JUnit is necessary. The JUnit mentioned here refers to a series of unit test functions provided by junit. jar downloaded from the Apache Foundation. These functions are obviously running on JDK. JDK is no longer available in Android.

Naturally, it cannot run JUnit. However, this does not prevent us from using JUnit to write unit tests. However, when running a unit test, you must use JDK to run and use java commands to start a Runner of JUnit. If Eclipse is used, you can create a new JUnit in Run Configuration. But remember to change the Android Library in Bootstrap Entries to jre on the Classpath tab and add junit. jar.

In this way, an Application is started when the program is started, and then the corresponding Activity is loaded during the Application running process, and the Activity requires an interface. But this is not the case with Instrumentation. You can understand Instrumentation as a tool class without a graphical interface and with startup capabilities, used to monitor other classes with the Target Package Declaration. Any class to be Instrumentation must inherit

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.