Eclipse下为Android项目进行单元测试(传智播客视频笔记)

AndroidManifest.xml文件内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.sinaapp.ssun"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk android:minSdkVersion="8" />

 <application
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name" >
 <activity
 android:name=".AndroidJunitActivity"
 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>
 
 <instrumentation android:name="android.test.InstrumentationTestRunner"
     android:targetPackage="com.sinaapp.ssun" />
</manifest>






Service.java源码:

package com.sinaapp.ssun;

public class Services {
	public void save(String str){
		str.substring(6);
	}
	
	public int add(int a,int b){
		return a+b; 
	}
}

Junit.java源码:

package com.sinaapp.test;

import junit.framework.Assert;
import android.test.AndroidTestCase;
import android.util.Log;

import com.sinaapp.ssun.Services;

public class Junit extends AndroidTestCase {
	public void testSave() throws Throwable{
		Services ser = new Services();
		ser.save(null);
	}
	
	public void testAdd() throws Throwable{
		Services ser = new Services();
		int actual = ser.add(1,3);
		Assert.assertEquals(3, actual);
	}
	
	public void TestLog(){
		Log.i("TestLOG", "hello");
	}
}

AndroidJunitActivity.java源码:

package com.sinaapp.ssun;

import android.app.Activity;
import android.os.Bundle;

public class AndroidJunitActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Eclipse下为Android项目进行单元测试(传智播客视频笔记)