編輯:高級開發
我們曾經和大家探討過全面剖析Java ME單元測試理念,其實在android上實現JUnit單元測試也不是很困難,主要是在配置文件和測試環境上將花費很長時間,下面從四步簡單講一下在android上實現Junit單元測試。
第一步:新建一個TestCase,記得要繼承androidTestCase,才能有getContext()來獲取當前的上下文變量,這在Android測試中很重要的,因為很多的android api都需要context。
Java代碼
- public class TestMath extends androidTestCase {
- private int i1;
- private int i2;
- static final String LOG_TAG = "MathTest";
- @Override
- protected void setUp() throws Exception {
- i1 = 2;
- i2 = 3;
- }
- public void testAdd() {
- assertTrue("testAdd failed", ((i1 + i2) == 5));
- }
- public void testDec() {
- assertTrue("testDec failed", ((i2 - i1) == 1));
- }
- @Override
- protected void tearDown() throws Exception {
- super.tearDown();
- }
- @Override
- public void testandroidTestCaseSetupProperly() {
- super.testandroidTestCaseSetupProperly();
- //Log.d( LOG_TAG, "testandroidTestCaseSetupProperly" );
- }
- }
第二步:新建一個TestSuit,這個就繼承Junit的TestSuite就可以了,注意這裡是用的addTestSuite方法,一開始使用addTest方法就是不能成功。
Java代碼
- public class ExampleSuite extends TestSuite {
- public ExampleSuite() {
- addTestSuite(TestMath.class);
- }
- }
第三步:新建一個Activity,用來啟動單元測試,並顯示測試結果。系統的androidTestRunner竟然什麼連個UI界面也沒有實現,這裡只是最簡單的實現了一個
Java代碼
- public class TestActivity extends Activity {
- private TextView resultVIEw;
- private TextView barVIEw;
- private TextView messageVIEw;
- private Thread testRunnerThread;
- private static final int SHOW_RESULT = 0;
- private static final int ERROR_FIND = 1;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentVIEw(R.layout.main);
- resultVIEw = (TextView)findViewById(R.id.ResultVIEw);
- barVIEw = (TextView)findViewById(R.id.BarVIEw);
- messageVIEw = (TextView)findViewById(R.id.MessageVIEw);
- Button lunch = (Button)findVIEwById(R.id.LunchButton);
- lunch.setOnClickListener(new VIEw.OnClickListener() {
- @Override
- public void onClick(VIEw v) {
- startTest();
- }
- });
- }
- private void showMessage(String message) {
- hander.sendMessage(hander.obtainMessage(ERROR_FIND, message));
- }
- private void showResult(String text) {
- hander.sendMessage(hander.obtainMessage(SHOW_RESULT, text));
- }
- private synchronized void startTest() {
- if (testRunnerThread != null
- && testRunnerThread.isAlive()) {
- testRunnerThread = null;
- }
- if (testRunnerThread == null) {
- testRunnerThread = new Thread(new TestRunner(this));
- testRunnerThread.start();
- } else {
- Toast.makeText(this,
- "Test is still running",
- Toast.LENGTH_SHORT).show();
- }
- }
- public Handler hander = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case SHOW_RESULT:
- resultVIEw.setText(msg.obj.toString());
- break;
- case ERROR_FIND:
- messageVIEw.append(msg.obj.toString());
- barVIEw.setBackgroundColor(Color.RED);
- break;
- default:
- break;
- }
- }
- };
- class TestRunner implements Runnable, TestListener {
- private Activity parentActivity;
- private int testCount;
- private int errorCount;
- private int failureCount;
- public TestRunner(Activity parentActivity) {
- this.parentActivity = parentActivity;
- }
- @Override
- public void run() {
- testCount = 0;
- errorCount = 0;
- failureCount = 0;
- ExampleSuite suite = new ExampleSuite();
- androidTestRunner testRunner = new androidTestRunner();
- testRunner.setTest(suite);
- testRunner.addTestListener(this);
- testRunner.setContext(parentActivity);
- testRunner.runTest();
- }
- @Override
- public void addError(Test test, Throwable t) {
- errorCount++;
- showMessage(t.getMessage() + "\n");
- }
- @Override
- public void addFailure(Test test, AssertionFailedError t) {
- failureCount++;
- showMessage(t.getMessage() + "\n");
- }
- @Override
- public void endTest(Test test) {
- showResult(getResult());
- }
- @Override
- public void startTest(Test test) {
- testCount++;
- }
- private String getResult() {
- int successCount = testCount - failureCount - errorCount;
- return "Test:" + testCount + " Success:" + successCount + " Failed:" + failureCount + " Error:" + errorCount;
- }
- }
- }
第四步:修改androidManifest.XML,加入
XML代碼
- XML version="1.0" encoding="utf-8"?>
- <manifest XMLns:android="http://schemas.Android.com/apk/res/android"
- package="com.test.sample"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
- <activity android:name=".TestActivity"
- 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>
- <uses-sdk android:minSdkVersion="4" />
- manifest>
目標:利用NDK 生成 SO 庫,使用 SO 庫進行 JNI 調用,在 android sdcard 創建文件並寫入數據。 工具:NDK1.5 R1, android
下面進行對android技術的全解讀,首先要了解下什麼是Android語言,所謂android語言:基於Linux內核的軟件平台和操作系統,早期由Google開發,後由
android 作為谷歌企業戰略的重要組成部分,這個戰略大大的提高了谷歌在中國手機界的市場,也大大的促進了整個手機行業的競爭性,下面就向大家介紹下android手機程序
對於android DEX文件進行優化,需要注意的一點是DEX文件的結構是緊湊的,但是我們還是要想方設法的進行提高程序的運行速度,我們就仍然需要對DEX文件進行進一步優