您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > junit
Android下Junit測(cè)試框架的使用
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2015/7/7 14:16:47 ] 推薦標(biāo)簽:單元測(cè)試 Junit

  package com.example.javabasic;
  public class CalculService {
  public int add(int x,int y){
  return x+y;
  }
  public static void main(String[] args){
  System.out.println("");
  }
  }
  直接運(yùn)行上面的代碼,會(huì)拋出以下異常。

  android 運(yùn)行在Dalvik虛擬機(jī)下,System.out 運(yùn)行在jvm下。
  android下的Junit框架,需要繼承 AndroidTestCase.
  package com.example.test;
  import com.example.javabasic.CalculService;
  import android.test.AndroidTestCase;
  public class TestCalcul extends AndroidTestCase {
  public void add(){
  CalculService service = new CalculService();
  int count = service.add(10, 12);
  // 斷言
  assertEquals(19, count);
  }
  }

  拋出異常,
  [2015-06-10 22:23:33 - JavaBasic] JavaBasic does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml
  沒(méi)有在AndroidManifest.xml中配置InstrumentationTestRunner。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.javabasic"
android:versionCode="1"
android:versionName="1.0" >
<!-- 指定測(cè)試包 -->
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.test">
</instrumentation>
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="22" />
<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"></uses-library>
<activity
android:name=".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>

軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd