Android实战简易教程-第五十枪(工具类的测试)

本文介绍如何在Android开发中自定义工具类,实现网络请求,并通过单元测试确保其可靠性。包括配置AndroidManifest.xml文件以启用测试,编写测试类及运行测试的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发中,为了提高开发效率,我们一般会自定义自己的工具类。为了保证项目的可靠性,在将工具类引入项目之前,我们一般都会对工具类进行单元测试,下面我们通过一个实例看一下如何搭建测试环境。
1.首先自定义一个工具类,这里我们自定义了一个连接图灵机器人API的网络测试类:
[java] view plaincopy
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";// URL  
  
    private 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文件:
[html] view plaincopy
<?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.下面开始编写测试类:
[java] view plaincopy
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再次运行:


这时我们的测试就通过了!

转载请保持链接:http://blog.youkuaiyun.com/chenyanpeng2003

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值