安卓中的单元测试
测试第三方的jar包
第一步:引入jar包
选中项目,右键选择properties,选择libraries,选择add libraries,选择Junit。
在order选项卡中把新添加的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)
下面是测试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() {
}
}