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("");
}
}
直接运行上面的代码,会抛出以下异常。
android 运行在Dalvik虚拟机下,System.out 运行在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
没有在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" >
<!-- 指定测试包 -->
<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>