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>


贴代码三: 写一个类extends AndroidTestCase

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