Android学习笔记之如何对应用进行单元测试
|
package cn.hao.service;
//业务类,待测试的两个方法
public class PersonaService {
public void save(String username){
String sub = username.substring(6);
}
public int add(int a,int b){
return a+b;
}
}
|
|
<?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>
|
|
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.save(null);
}
public void testAdd() throws Exception {
PersonaService service = new PersonaService();
int actual = service.add(1, 2);
Assert.assertEquals(3, actual);
}
}
|
