Step 1: Add the following two pieces of code to androidmanifest. xml:
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "com. PCCW "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 = ". mainactivity "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <cat Egory Android: Name = "android. Intent. Category. launcher"/> </intent-filter> </activity> <! -Add code 1 --> <uses-library Android: Name = "android. Test. Runner"/> </Application> <! -Add code 2 --> <instrumentation Android: Name = "android. test. instrumentationtestrunner "Android: targetpackage =" com. PCCW "Android: Label =" AAA "/> </manifest>
1.
<Uses-library Android: Name = "android. Test. Runner"/> indicates introducing some dependent libraries in the unit test framework.
2.
<Instrumentation Android: Name = "android. test. instrumentationtestrunner "Android: targetpackage =" com. PCCW "Android: Label =" AAA "/> indicates the Startup Device for configuring the unit test framework. There are several types of start devices. You can choose from them. Generally, we use the above.
3.
The targetpackage is the same as the preceding package, indicating that the unit test framework and the current application are in the same process.
Step 2: Write the business logic, that is, the module to be tested
public class PersonService {public void save(String name){String sub = name.substring(6);}public int add(int a, int b){return a+b;}}
Step 3: Write unit test code
public class PersonServiceTest extends AndroidTestCase {public void testSave() throws Exception {PersonService service = new PersonService();service.save(null);}public void testAdd() throws Exception {PersonService service = new PersonService();int result = service.add(1, 2);Assert.assertEquals(3, result);}}
Step 4: Open the outline window in eclipse. All methods of the unit test class are displayed.
Right-click the method to be tested, select Run as, and then choose Android JUnit test. If an exception or error occurs, the following occurs:
If it is normal, it will be as follows: