In development, in order to improve the efficiency of development, we usually customize our own tool classes. In order to ensure the reliability of the project, we usually do unit testing of the tool class before we introduce the tool class into the project, so let's take a look at how to build the test environment.
1. First customize a tool class, where we customize a network test class that connects the Turing Man 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 = "eb5a484b007db73 d44cb35300ef58c73 ";p rivate static string requeststring = URLString +"? key= "+ key +" &info= ";p ublic static string DoG ET (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 (conn.setconnecttimeout); is = Conn.getinputStream (); int len = -1;byte[] buf = new Byte[128];baos = new Bytearrayoutputstream (); while (len = Is.read (BUF))! =-1) {BA Os.write (buf, 0, Len);} Baos.flush (); result = new String (Baos.tobytearray ());} catch (Malformedurlexception e) {e.printstacktrace ()} catch (IOException e) {e.printstacktrace ();} finally {try {if (Bao s! = null) {Baos.close ();} if (is = null) {Is.close ();}} catch (IOException e) {e.printstacktrace ();}} return result;}}
2. We need to configure the Androidmanifest.xml file:
<?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= "/> <application android:allowbackup=" tr UE "android:icon=" @drawable/ic_launcher "android:label=" @string/app_name "android:theme=" @style/appth EME "> <uses-library android:name=" Android.test.runner "/> <!--unit Test--<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 A Ndroid:name= "Android. Test. Instrumentationtestrunner "Android:label=" This is Test "android:targetpackage=" Com.yayun.chatrobot "> </instrumentation></manifest>
the locations configured above are labeled:
3. Start writing the test class below:
Import Com.yayun.chatrobot.utils.netutil;import android.test.androidtestcase;import android.util.Log;/** * Inherit androidtestcase * * @author Administrator * */public class Testnetutil extends Androidtestcase {public void testsend Info () {String res = netutil.doget ("Hello! "); LOG.E ("Tag", res); res = Netutil.doget ("Who Are you!") "); LOG.E ("Tag", res);}}
4. Select the test method Testsendinfo, right-click the android unit test:
Run this is an error:
The original is not configured network permissions: <uses-permission android:name= "Android.permission.INTERNET"/>
Configure and then run again as Step 4:
And then our test passed!
Like friends please pay attention to me! Thank you
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Combat Simple Tutorial-the 50th Gun (tool class test)