Android(java)學習筆記165:Android下編寫單元測試代碼(Junit Test)

來源:互聯網
上載者:User

標籤:

編寫android應用的時候,往往我們需要編寫一些商務邏輯實作類別,但是我們可能不能明確這個商務邏輯是否可以成功實現,特別是邏輯代碼體十分巨大的時候,我們不可能一行一行檢查自己的代碼,為瞭解決這樣的問題就出現了:

Android下編寫單元測試代碼-----Junit Test

      測試邏輯是:在Eclipse我們待測試專案中編寫測試代碼,然後運行測試代碼,系統會把代碼布署到模擬器或者真機中,代碼運行之後,會反饋測試結果給Eclipse,使用者就知道商務邏輯類是否可以成功實現。

 

首先我們明確Android下編寫單元測試代碼的步驟:

  1. 編寫測試類別,extends AndroidTestCase
  2. 編寫測試方法, 修飾符是public,直接拋出異常給測試架構 throws Exception,記得讓測試方法直接拋出throws異常,不能在方法內部try...catch,這是捕獲異常,我們這裡必須拋出異常,讓使用者知道
  3. 進行斷言
  4. 資訊清單檔配置 在application節點配置<uses-library android:name="android.test.runner"/> 在manifest節點裡面配置<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="當前應用程式的包名">
  5. 運行測試案例。綠條測試通過,紅條測試失敗。

 

案例:

1.首先我們建立一個android項目,命名為:01_測試案例,如:

 

2.這裡我們還沒有對主程式進行編寫,我們先編寫一個義務邏輯類CalcService.java,放到我們業務實現包com.itheima.junit.service,代碼內容如下:

package com.itheima.junit.service;/** * 計算機的業務類 完成加減乘除的操作 * */public class CalcService {    /**     * 兩個數字相加     * @param x     * @param y     */    public int add(int x,int y){        return x+y;    }    }

這裡就是一個簡單實現加法的邏輯方法

 

3.前面我們已經編寫好了待測試的商務邏輯類,下面自然就是編寫測試代碼,這個測試代碼我們再建立一個包:com.itheima.junit.test包中,這個測試類別我們命名為

TestCalService.java,這裡這樣命名就會見名知意,很明顯知道這是測試CalService用途的。測試代碼內容如下:

package com.itheima.junit.test;import com.itheima.junit.service.CalcService;import android.test.AndroidTestCase;/** * 1. android下的測試代碼 一定記得繼承一個父類AndroidTestCase */public class TestCalcService extends AndroidTestCase{        /**     * 2.編寫測試方法記得 說有的測試方法 都應該是public     * 記得讓測試方法直接拋出異常,不能在方法內部try...catch,這是捕獲異常,我們這裡必須拋出異常,讓使用者知道     * 根據需求進行斷言操作 assert     */    public void testAdd() throws Exception{        CalcService calcService = new CalcService();        int result = calcService.add(3, 5);        assertEquals(8, result);    }}

 

4.配置AndroidManifest.xml資訊清單檔如下:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.itheima.junit"    android:versionCode="1"    android:versionName="1.0" >    <instrumentation android:name="android.test.InstrumentationTestRunner"        android:targetPackage="com.itheima.junit"></instrumentation>    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <uses-library android:name="android.test.runner"/>        <activity            android:name="com.itheima.junit.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>    </application></manifest>

 

 

5.進入J2EE模式,如下:

運行之後,等待系統會布署代碼到模擬器或真機上,在模擬器或真機上面運行成功之後,會反饋測試結果該Eclipse,報錯的會也會告知Eclipse,成功了也會告知Eclipse。

Android(java)學習筆記165:Android下編寫單元測試代碼(Junit Test)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.