Android Unit Test JUnit (i)

Source: Internet
Author: User

1, establish an Android project in Eclips, the specific information is as follows:

2, configure the Unit test environment, open androidmanifest.xml, the specific code is as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.milan.junit"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "+"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/apptheme" >        <ActivityAndroid:name= "Com.milan.junit.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <!--using the Unit test library -        < uses-libraryAndroid:name= "Android.test.runner"/>    </Application>    <!--declaring Android frames and target test packages -    <InstrumentationAndroid:name= "Android.test.InstrumentationTestRunner"Android:targetpackage= "Com.milan.junit" /></Manifest>

Description

1,<uses-library android:name= "Android.test.runner"/> /c13> This sentence, put in application inside.

2.Android:targetpackage This property, typically the package name of the application, must have the same name as the manifest node in the XML.

3, create a package Com.milan.service, this package is then created a class person, used for testing when called.

 Package Com.milan.service;  Public class Person {    publicint  getage (String age) {        returnNew  Integer (age);    }}

4, under the Com.milan.junit package, build a Persontest class, the code is as follows:

 PackageCom.milan.junit;ImportCom.milan.service.Person;Importandroid.test.AndroidTestCase; Public classPersontestextendsAndroidtestcase {///Use Case 1, incoming non-numeric     Public voidtest01 () { person person=NewPerson (); Person.getage ("Milan"); }    //use case 2, incoming numbers     Public voidtest02 () { person person=NewPerson (); Person.getage ("1"); }}

5. Select Persontest.java, right-select Run as to select the Android Junit test running unit test,

As you can see, the test01 test result is a failure. The reason for failure is: Java.lang.NumberFormatException:Invalid int: "Milan",

The test results for test02 are passed.

If you do not have Junit run results, you can select Open in Show View--other--java--junit under Eclips's Window menu.

6. Run a single test case

Eclips the Window menu, select Show View--other--general--outline to open the outline view and right-click the test case to run a single test case.

Similarly, if you need to execute a test case for the entire project, you can select the entire project and run it with the Android Junit test.

7. Assertion

When we get the results of the program running, we want JUnit to automatically determine whether the actual and expected results are consistent. At this point we can use JUnit's own assertions.

Modify the Persontest class to the following code:

 PackageCom.milan.junit;ImportCom.milan.service.Person;Importandroid.test.AndroidTestCase; Public classPersontestextendsAndroidtestcase {///Use Case 1, incoming non-numeric     Public voidtest01 () { person person=NewPerson (); intI= person.getage ("Milan"); Assertequals (1, i); }    //use case 2, passing in a number, asserting that there is a message     Public voidtest02 () { person person=NewPerson (); intI= person.getage ("1"); Assertequals ("Incoming number", 2, i); }    //use case 2, passing in numbers, asserting no message     Public voidtest03 () { person person=NewPerson (); intI= person.getage ("1"); Assertequals (1, i); }}

Then run the result as:

If a message is passed in, once the use case does not pass, the message is listed in the error details on the right.

8. Other assertions

JUnit provides a rich range of assertions to meet our different requirements. By holding down the CTRL key and clicking Assertequals in the code, you can see the argument types of the Assert functions that are supported in the Assert class:

You can try more when you are free. There is no explanation here.

Additional Information

If the test run failed:unable to find instrumentation the target package error appears

it's because of android:targetpackage= . "Com.milan.junit" must and

<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package="Com.milan.junit"
package with the same name

Android Unit Test JUnit (i)

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.