Android单元测试jUnit
来源:互联网 发布:中草药软件 编辑:程序博客网 时间:2024/06/05 07:26
慕课网的智能机器人的那个项目视频中学到的。
写了一个工具类(HttpUtils),可以写一个单元测试去测试其类的功能是否正确,而不必等布局文件写好再去测试。
贴代码一:HttpUtils工具类
public class HttpUtils { private static final String URL="http://www.tuling123.com/openapi/api"; private static final String API_KEY="950e94cd5cf0a9de9a66d68a6c240502"; public static String doGet(String msg){ String result =""; String url =setParams(msg); Log.i("TAG","zzj--url地址-->>"+url); InputStream is=null; ByteArrayOutputStream baos=null; try {java.net.URL urlNet = new java.net.URL(url);HttpURLConnection conn =(HttpURLConnection)urlNet.openConnection();conn.setReadTimeout(5000);conn.setConnectTimeout(5000);conn.setRequestMethod("GET");is =conn.getInputStream();int len=-1;byte[]buf = new byte[128];baos =new ByteArrayOutputStream();while((len=is.read(buf))!=-1){baos.write(buf,0,len);}result = new String(baos.toByteArray());baos.flush();Log.i("TAG","-HTTP-1>>"+result);} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{if(baos!=null){try {baos.close();} catch (IOException e) {e.printStackTrace();}}if(is!=null){try {is.close();} catch (IOException e) {e.printStackTrace();}}} Log.i("TAG","-HTTP->2>"+result); return result; }private static String setParams(String msg) {String url="";try {url=URL+"?key="+API_KEY+"&info="+URLEncoder.encode(msg,"UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}return url;}}
贴代码二: 要在AndroidManifest.xml中写两个标签<uses-library /> <instrumentation />
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.robot" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.INTERNET"/> <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"/> <activity android:name="com.example.robot.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> <instrumentation android:targetPackage="com.example.robot" android:label="this is a test" android:name="android.test.InstrumentationTestRunner"></instrumentation></manifest>
public class TestHttpUtils extends AndroidTestCase { public void testSendInfo(){ String res =HttpUtils.doGet("给我讲个笑话"); Log.i("TAG","z-笑话->>"+res); res = HttpUtils.doGet("给我讲个鬼故事"); Log.i("TAG","z-故事->>"+res); }}
使用方法: 鼠标选中testSendInfo()方法的位置,右键 run as -> Android jUnit Test
0 0
- Android:Junit 单元测试方法
- Android Junit单元测试
- Android Junit单元测试
- android中junit单元测试
- Android 使用Junit单元测试
- Android单元测试Junit
- Android:单元测试Junit配置
- Android单元测试 Junit(1)
- junit实现android单元测试
- android junit单元测试
- android JUnit单元测试
- android JUnit单元测试
- Android单元测试jUnit
- android junit单元测试
- Android单元测试之JUnit
- Android Junit 单元测试
- Android JUnit单元测试
- Android中的Junit单元测试
- A. Forgotten Episode(水题)
- hdu 4111 Alice and Bob(博弈)
- poj 1422 最小路径覆盖
- HDU Clear All of Them I(状态压缩+DP+记忆搜索)
- 个人对多线程的总结
- Android单元测试jUnit
- 防止进程重新启动-keepalived源码解析
- java内存管理
- Hduoj3874 【离线+树状数组】
- 2014-11-8Android学习------Android抽屉效果的实现案例--------动画Animation学习篇
- 构建高性能Web站点(修订版)笔记
- JSP九大内置对象
- HDU 4786 Fibonacci Tree 并查集+生成树=kruskal
- H264 GOP 扫盲