Simple tutorial for Android-50th gun (Tool Test) and android Tool

Source: Internet
Author: User

Simple tutorial for Android-50th gun (Tool Test) and android Tool

In development, we generally customize our own tool classes to improve development efficiency. To ensure the reliability of the project, we usually perform unit tests on the tool class before introducing the tool class to the project. Let's take an example to see how to build a test environment.

1. First, define a tool class. Here we have customized a network test class that connects to the Turing robot 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. 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 = "17"/> <application android: allowBackup = "true" android: icon = "@ drawable/ic_launcher" android: label = "@ string/app_name" android: theme = "@ style/AppTheme"> <uses-library android: name = "android. t Est. 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 android: name =" android. test. instrumentationTestRunner "android: label =" This is Test "android: targetPackage =" com. yayun. chatrobot "> </instrumentation> </manifest>

Mark the position configured above:


3. Write the test class as follows:

Import com. yayun. chatrobot. utils. netUtil; import android. test. androidTestCase; import android. util. log;/*** inherits AndroidTestCase ** @ author Administrator **/public class TestNetUtil extends AndroidTestCase {public void testSendInfo () {String res = NetUtil. doGet ("Hello! "); Log. e (" Tag ", res); res = NetUtil. doGet (" who are you! "); Log. e (" Tag ", res );}}
4. Right-click testSendInfo and choose android unit test:

This is an error:


Originally, no network permission was configured: <uses-permission android: name = "android. permission. INTERNET"/>

After configuration, follow Step 4 to run again:


Now our test is passed!


If you like me, please follow me! Thank you.




Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.