Android中的junit单元测试

安卓中的单元测试

测试第三方的jar包

第一步:引入jar包

选中项目,右键选择properties,选择libraries,选择add libraries,选择Junit。
在order选项卡中把新添加的Junit打钩。


第二步:配置文件中位置信息

在清单文件中添加两个节点。instrumentation在application外面添加;uses-library在application内,activity外添加。
 
<instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.itheima.impxjq" >
    </instrumentation>


    <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"
            android:required="true" />


        <activity
            android:name="com.itheima.impxjq.MainActivity"
            android:label="@android:style/Theme.Light.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


第三步:建立测试类测试代码

新建一个junit test case 测试类



创建好后点击next,不要点击finish。选择要测试的方法。


点击finish完成。下面就可以编写测试代码,测试jar包了。
下面是测试XStream的测试代码,Xstream是javabean和xml相互转化的开源工具类。
在这里主要测试了两个方法toXML(Object)和fromXML(String)
public class xstreamTest extends AndroidTestCase {
	@Test
	public void testToXMLObject() {
		XStream x=new XStream();
		x.alias("QQMessage", QQMessage.class);
		QQMessage m=new QQMessage();
		m.type=QQMessageType.MSG_TYPE_CHAT_P2P;
		m.content="今晚有空没";
		m.fromNick="老王";
		m.from=123;
		m.to=10086;
		String xml=x.toXML(m);
		QQMessage m2=(QQMessage) x.fromXML(xml);
		System.out.println(m2.fromNick);
	}
	@Test
	public void testFromXMLReader() {
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值