Android: Unit Test JUnit Configuration

Source: Internet
Author: User

In real-world development, the process of developing Android software needs to be constantly tested. Using the JUnit test framework, the side is the required technology for regular Android development, and in junit you can get components that can simulate the correctness of sending events and detecting program processing ....

First step: First add the following code to the Androidmanifest.xml:

    <?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "Http://schemas.android.com/apk/res/andro          ID "package=" hb.learn.junit "android:versioncode=" 1 "android:versionname=" 1.0 "> &LT;USES-SDK android:minsdkversion= "8"/> <application android:icon= "@drawable/icon" android:label= "@str Ing/app_name "> <!--to import the package you need to use in this app, put it inside application activity outside---&LT;USES-LIBR ary android:name= "Android.test.runner"/> <activity android:name= ".                      Junittestactivity "android:label=" @string/app_name "> <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name                        = "Android.intent.category.LAUNCHER"/> </intent-filter> </activity>            </application>        <!--Remember this one is to be placed outside the application, otherwise the configuration error message--<instrumentation android:name= "Android.test.Instrume Ntationtestrunner "android:targetpackage=" Hb.learn.junit "android:label=" Tests for My App "/> </ma   Nifest>

The packages specified above Targetpackage the same package as the application. is the package name in which the test class resides;

Step Two: Write the Unit test code (select the method you want to test, right-click "Run as"-"Android Junit Test"):

[Java]View Plaincopyprint?
    1. Import Android.test.AndroidTestCase;
    2. Import Android.util.Log;
    3. Public class XMLTest extends Androidtestcase {
    4. public void testsomething () throws Throwable {
    5. Assert.asserttrue (1 + 1 = = 3);
    6. }
    7. }

Description of the error message

In the process of running the test example, you will also encounter a number of error hints, summarized as follows:

after clicking "Android JUnit Test", an "Android Launch" error message appears, as follows
At the same time, the following information is output in the console panel of the program:
Error:application does not specify a android.test.InstrumentationTestRunnerinstrumentation or does not declare USES-LIBR ary Android.test.runner.

The cause of the error could be: Androidmanifest.xml configuration error. So what exactly does the androidmanifest.xml need to configure, and the following one by one shows you:

1. Add a statement to the <application> quote Android.test.runner

[HTML]View Plaincopyprint?
    1. <!--Import the package you need to use in this app, put it inside the application activity outside--
    2. <uses-library android:name="Android.test.runner" />

2. Then add instrumentation information to the <manifest>

[HTML]View Plaincopyprint?
    1. <!--Remember that this one needs to be placed outside the application, otherwise the configuration error message will appear--
    2. <instrumentation android:name="Android.test.InstrumentationTestRunner"
    3. android:targetpackage="Hb.learn.junit" android:label="Tests for My App" />


According to your own program, configure the above information in the Androidmanifest.xml file. If the information above is configured correctly, right-click the project, select Run as\run configurations, and select project in the Android JUnit Test option, you will see the following interface:


In the list box options after instrumentation runner, we see Android.test.InstrmentationTestRunner and are in the current selection state. If there are no options in this box, there is a problem with the Androidmanifest.xml configuration.

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.