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"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Hb.learn.junit"Android:versioncode= "1"Android:versionname= "1.0">    <USES-SDKandroid:minsdkversion= "8" />    <ApplicationAndroid:icon= "@drawable/icon"Android:label= "@string/app_name">            <!--Import the packages you need to use in this application, and put them in the application outside activity -        <uses-libraryAndroid:name= "Android.test.runner" />                <ActivityAndroid:name=". Junittestactivity "Android:label= "@string/app_name">            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>            </Application>        <!--Remember this one's going to be outside the application, or you'll get a configuration error message -    <InstrumentationAndroid:name= "Android.test.InstrumentationTestRunner"Android:targetpackage= "Hb.learn.junit"Android:label= "Tests for My App" /></Manifest>

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"):

Import android.test.AndroidTestCase; Import Android.util.Log;  Public class extends androidtestcase {     publicvoidthrows  throwable {        Assert.asserttrue (1 + 1 = = 3);}     }

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

<!---        <android:name = "Android.test.runner"  />

2. Then add instrumentation information to the <manifest>

<!---    <android:name = " Android.test.InstrumentationTestRunner "          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 option 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.

Android: Unit Test JUnit 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.