Android實戰簡易教程-第五十槍(工具類的測試),android工具類

來源:互聯網
上載者:User

Android實戰簡易教程-第五十槍(工具類的測試),android工具類

在開發中,為了提高開發效率,我們一般會自訂自己的工具類。為了保證項目的可靠性,在將工具類引入項目之前,我們一般都會對工具類進行單元測試,下面我們通過一個執行個體看一下如何搭建測試環境。

1.首先自訂一個工具類,這裡我們自訂了一個串連圖靈機器人API的網路測試類別:

package com.yayun.chatrobot.utils;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLEncoder;public class NetUtil {private final static String URLSTRING = "http://www.tuling123.com/openapi/api";// URLprivate final static String KEY = "eb5a484b007db73d44cb35300ef58c73";private static String requestString = URLSTRING + "?key=" + KEY + "&info=";public static String doGet(String msg) {String result = "";String url = "";try {url = requestString + URLEncoder.encode(msg, "UTF-8");} catch (UnsupportedEncodingException e1) {e1.printStackTrace();}InputStream is = null;ByteArrayOutputStream baos = null;try {URL urlNet = new URL(url);HttpURLConnection conn = (HttpURLConnection) urlNet.openConnection();conn.setRequestMethod("GET");conn.setReadTimeout(5000);conn.setConnectTimeout(5000);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);}baos.flush();result = new String(baos.toByteArray());} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (baos != null) {baos.close();}if (is != null) {is.close();}} catch (IOException e) {e.printStackTrace();}}return result;}}



2.我們需要配置AndroidManifest.xml檔案:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.yayun.chatrobot"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <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=".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:name="android.test.InstrumentationTestRunner"        android:label="This is Test"        android:targetPackage="com.yayun.chatrobot" >    </instrumentation></manifest>

上面配置的位置標記一下:


3.下面開始編寫測試類別:

import com.yayun.chatrobot.utils.NetUtil;import android.test.AndroidTestCase;import android.util.Log;/** * 繼承AndroidTestCase *  * @author Administrator * */public class TestNetUtil extends AndroidTestCase {public void testSendInfo() {String res = NetUtil.doGet("你好!");Log.e("Tag", res);res = NetUtil.doGet("你是誰!");Log.e("Tag", res);}}
4.選擇測試方法testSendInfo,右鍵選擇android單元測試:

運行這是報錯:


原來是沒有配置網路許可權:<uses-permission android:name="android.permission.INTERNET"/>

配置後按照步驟4再次運行:


這時我們的測試就通過了!


喜歡的朋友請關注我!謝謝




著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.