First, join the configuration item:
1. In application, add the reference test class library code to the activity item:
<uses-libraryAndroid:name= "Android.test.runner" />
2. Add the "Test entry" code to the application entry:
<InstrumentationAndroid:name= "Android.test.InstrumentationTestRunner"Android:targetpackage= "Com.juniux.junittest"Android:label= "Test My App" />
<!--The Android:label property can be ignored -
Second, the editing test class:
Note: This class inherits the Androidtestcase class and is recommended to be named: "Class name to test" +test; test method Name: test+ "method name to test" and throws exception.
PackageCom.juniux.test;
ImportJunit.framework.Assert;
ImportCom.juniux.services.PersonService;
ImportAndroid.test.AndroidTestCase;
PublicclassPersonservicetestextendsAndroidtestcase {
PublicvoidTestsave ()throwsException {
Personservice Service =NewPersonservice ();
Service. Save (NULL);
}
PublicvoidTestadd ()throwsException
{
Personservice Service =NewPersonservice ();
intresult = Service. ADD (1, 2);
Assert.assertequals (3, result);
}
}
Three: Enable testing
Right-click the method you want to test in the outline list and select the Android JUnit Test start test, RunAs.