- 第一步:建立一个工程:(包名:edu.cquptzx.Junit)
--------------------MainActivity.java----------------
package edu.cquptzx.Junit;
import edu.cquptzx.Junit.R;
import android.app.Activity;
import android.os.Bundle;
publicclass MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
-
第二步:新建一个包(edu.cquptzx.service),存放业务类(PersonServce.java)
-------------------PersonServce.java----------------
package edu.cquptzx.service;
publicclass PersonService
{
publicvoid save ()
{
// TODO Auto-generated method stub
String str = "Android";
Integer mintger= new Integer(str);
}
}
-
第三步:配置单元测试环境:
------------------- AndroidManifest.xml----------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.cquptzx.Junit"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- 使用单元测试库 -->
<uses-library android:name="android.test.runner" />
<!-- 使用单元测试库 -->
<activity
android:label="@string/app_name"
android:name="edu.cquptzx.Junit.MainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!-- 声明Android框架和目标测试包 -->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="edu.cquptzx.Junit"
android:label="hello Androidtest"
/>
<!-- 声明Android框架和目标测试包 -->
</manifest>
- 第四步:建立一个单元测试类(PersonServiceTest.java)
[注意]1.单元测试类需要继承类AndroidTestCase .
2.单元测试类包名为:edu.cquptzx.Junit;
------------------- PersonServiceTest.java----------------
package edu.cquptzx.Junit;
import edu.cquptzx.service.PersonService;
import android.test.AndroidTestCase;
publicclass PersonServiceTest extends AndroidTestCase
{
publicvoid testSave() throws Throwable
{
PersonService ps = new PersonService();
ps.save();
}
}
-
第五步:进行单元测试:
查看console窗口信息:
[2012-09-07 00:32:00 - HelloJunit] ------------------------------
[2012-09-07 00:32:00 - HelloJunit] Android Launch!
[2012-09-07 00:32:00 - HelloJunit] adb is running normally.
[2012-09-07 00:32:00 - HelloJunit] Performing android.test.InstrumentationTestRunner JUnit launch
[2012-09-07 00:32:00 - HelloJunit] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'a30t'
[2012-09-07 00:32:00 - HelloJunit] Uploading HelloJunit.apk onto device 'emulator-5554'
[2012-09-07 00:32:00 - HelloJunit] Installing HelloJunit.apk...
[2012-09-07 00:32:04 - HelloJunit] Success!
[2012-09-07 00:32:04 - HelloJunit] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2012-09-07 00:32:04 - HelloJunit] Collecting test information
[2012-09-07 00:32:06 - HelloJunit] Sending test information to Eclipse
[2012-09-07 00:32:06 - HelloJunit] Running tests...
[2012-09-07 00:32:08 - HelloJunit] Test run finished
测试结果:
- 第六步:修改业务代码,重新测试:
-------------------PersonServce.java----------------
package edu.cquptzx.service;
publicclass PersonService
{
publicvoid save ()
{
// TODO Auto-generated method stub
String str = "123";
Integer mintger= new Integer(str);
}
}
For more details , contacts me by :