在开发中,为了提高开发效率,我们一般会自定义自己的工具类。为了保证项目的可靠性,在将工具类引入项目之前,我们一般都会对工具类进行单元测试,下面我们通过一个实例看一下如何搭建测试环境。
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";// 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文件:
- <?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);
- }
- }
运行这是报错:
原来是没有配置网络权限:<uses-permission android:name="android.permission.INTERNET"/>
配置后按照步骤4再次运行:
这时我们的测试就通过了!