Android learning notes: how to perform unit tests on Applications

Source: Internet
Author: User

Development Environment:

Win XP + eclipse-jee-helios (version 3.6) + ADT (version 10.0.1) + Android SDK (version 10 );

Test environment for simulators and real machines: Android2.2


The Junit testing framework can be used during Android software development. You can obtain components in Junit to simulate the correctness of sending events and testing program processing.

Step 1: Create the business class to be tested in the new project. In the cn. hao. service package, the Code is as follows:

Package cn. hao. service; // business class, two methods to be tested: public class PersonaService {public void save (String username) {String sub = username. substring (6);} public int add (int a, int B) {return a + B ;}}

NOTE: For the save () method, if the parameter is null, this method will encounter an error. For the add () method, we test whether the addition result returned by the addition is correct.

Step 2: Introduce a unit test environment for Applications

Add the following code to AndroidManifest. xml:

<Uses-library android: name = "android. test. runner"/>

<Instrumentation android: name = "android. test. instrumentationTestRunner"
Android: targetPackage = "cn. hao. JunitTest" android: label = "Test for My App"/>

The introduced location is as follows:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="cn.hao.JunitTest"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name="cn.hao.test.MainActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <uses-library android:name="android.test.runner"/>    </application>                                                                                                                                                                                                                 <instrumentation android:name="android.test.instrumentationTestRunner"        android:targetPackage="cn.hao.JunitTest" android:label="App Test"    /></manifest>

Note: unit tests are used in projects to check the correctness of programs and processing results.

Step 3: Create a class and test the business class. The Code is as follows:

Package cn. hao. junit; import junit. framework. assert; import cn. hao. service. personaService; import android. test. androidTestCase; public class PersonServiceTest extends AndroidTestCase {public void testSave () throws Exception {PersonaService service = new PersonaService (); // new service as the test object. save (null);} public void testAdd () throws Exception {PersonaService service = new PersonaService (); int actual = service. add (1, 2); Assert. assertEquals (3, actual );}}

Note:This class must inherit the unit test framework parent classAndroid. test. AndroidTestCaseClass, the test method is best to throw an exception to the test framework. MethodAssert. assertEquals (3, actual)Parameter 3 is the expected (theoretically) returned result, and actual is the actually returned result.

Step 4: run the test class

In the OutLine view of the OutLine, right-click the Test method and choose Run As> Android Junit Test. The project is automatically deployed to the simulator, and the Test result is displayed in color, green indicates that the method is correct. Otherwise, the method is incorrect. Eclipse will provide a detailed description. You can view the error information according to the help document.

If you test the testSave () method, the following prompt is displayed: 650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/10052631I-0.png "title =" ff.png "/>

Of course, save () starts to take the sub-string from the sixth bit. However, if the current parameter of this method is null, a null pointer exception will occur.

This article from the "Flowers bloom" blog, please be sure to keep this source http://020618.blog.51cto.com/6098149/1293560

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.