国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android單元測試之對Activity的測試示例

2019-12-12 02:14:54
字體:
來源:轉載
供稿:網友

上一篇文章已經介紹了單元測試的作用和簡單示例,如果不了解的讀者可以先閱讀上一篇Android單元測試-作用以及簡單示例。 

這篇文章主要介紹常見的Activity中的測試。

對Acitivity的測試

對于Activity,我們大致有兩種測試需求:

1、在Activity正常啟動后,查看界面布局是否正確,包括View的點擊事件等是否正確。

2、需要在Activity啟動前完成各種數據的部署,然后查看Activity的效果。

對于這兩種需求,筆者分別做了兩個示例解說:

1、檢測一個布局中的button和TextView是否正確。

2、從網絡動態獲取String到Activity界面顯示,并且這個圖片的URL是由Intent傳遞過來的。

環境部署

首先要導入expresso-core的包,如下:

dependencies {  // Other dependencies ...  androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'}

當然在目前的項目架構中一般已經自動導入了這個包,所以不需要自己導入,筆者項目中自動導入的包如下如下:

dependencies {  compile fileTree(include: ['*.jar'], dir: 'libs')  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {    exclude group: 'com.android.support', module: 'support-annotations'  })  compile 'com.android.support:appcompat-v7:26.0.0-alpha1'  compile 'com.android.support.constraint:constraint-layout:1.0.2'  testCompile 'junit:junit:4.12'}

項目結構如下:

布局View的測試:

package com.example.xujiajia_sx.myexpressotest;import android.app.Activity;import android.os.Bundle;import android.support.annotation.Nullable;import android.view.View;import android.widget.Button;import android.widget.TextView;/** * Created by xujiajia_sx on 2017/8/14. */public class SimpleViewActivity extends Activity{  private TextView tv;  private Button btn;  @Override  protected void onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.act_simple_view);    initView();  }  private void initView() {    tv=findViewById(R.id.tv_simple_view);    btn=findViewById(R.id.btn_simple_view);    tv.setText("111");    btn.setText("222");    btn.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View view) {        tv.setText("777");      }    });  }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical">  <TextView    android:id="@+id/tv_simple_view"    android:layout_width="wrap_content"    android:layout_height="wrap_content"     />  <Button    android:id="@+id/btn_simple_view"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    /></LinearLayout>
package com.example.xujiajia_sx.myexpressotest;import android.support.test.rule.ActivityTestRule;import org.junit.Rule;import org.junit.Test;import static android.support.test.espresso.Espresso.onView;import static android.support.test.espresso.action.ViewActions.click;import static android.support.test.espresso.assertion.ViewAssertions.matches;import static android.support.test.espresso.matcher.ViewMatchers.withId;import static android.support.test.espresso.matcher.ViewMatchers.withText;/** * Created by xujiajia_sx on 2017/8/14. */public class SimpleViewTest {  @Rule  public ActivityTestRule<SimpleViewActivity> mActivityTestRule =      new ActivityTestRule<SimpleViewActivity>(SimpleViewActivity.class);  @Test  public void textViewTest() throws Exception {    onView(withId(R.id.tv_simple_view))        .check(matches(withText("111")));  }  @Test  public void buttonTest() throws Exception {    onView(withId(R.id.btn_simple_view))        .check(matches(withText("222")))        .perform(click());    onView(withId(R.id.tv_simple_view))        .check(matches(withText("777")));  }}

測試主要邏輯:

1、首先要使用ActivityTestRule初始化你要測試的Activity。

2、編寫測試方法,測試View是否是我們預期的樣子。

兩個測試方法邏輯如下:

textViewTest():

在Activity中查找id為tv_simple_view的View,檢查它的text是否為“111”。

buttonTest():

在Activity中查找id為btn_simple_view的View,檢查它的text是否為“222”。然后執行點擊事件,點擊事件的邏輯是在Activity的OnCreate中設置的,是把TextView的text設置為777。在執行完點擊事件后,測試方法中繼續測試TextView的text是否為“777”。

讀者可能閱讀到對View的測試非常陌生,不用擔心,此處主要要理解測試的邏輯即可,筆者會在下篇文章具體講解View的各種測試方法。

網絡獲取String的Activity測試:

package com.example.xujiajia_sx.myexpressotest;import android.app.Activity;import android.os.Bundle;import android.support.annotation.Nullable;import android.widget.TextView;/** * Created by xujiajia_sx on 2017/8/14. */public class ActTestActivity extends Activity{  private TextView tv;  @Override  protected void onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.act_act_test);    initView();  }  private void initView() {    tv= findViewById(R.id.tv_act_test);    new Thread(new Runnable() {      @Override      public void run() {        String url =getIntent().getStringExtra("url");        final String s=mHttpClient.getInstance().get(url);        runOnUiThread(new Runnable() {          @Override          public void run() {            tv.setText(s);          }        });      }    }).start();  }}
package com.example.xujiajia_sx.myexpressotest;/** * Created by xujiajia_sx on 2017/8/14. */public class mHttpClient {  private static HttpURLConnectionClient mClient = null;  public static void setClient(HttpURLConnectionClient client) {    mClient = client;  }  public static HttpURLConnectionClient getInstance() {    return mClient;  }}
package com.example.xujiajia_sx.myexpressotest;import android.util.Log;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import static android.content.ContentValues.TAG;/** * Created by xujiajia_sx on 2017/8/14. */public class HttpURLConnectionClient {  public String get(String url) {    HttpURLConnection conn = null;    try {      URL mURL = new URL(url);      conn = (HttpURLConnection) mURL.openConnection();      conn.setRequestMethod("GET");      conn.setConnectTimeout(2000);      conn.connect();      InputStream is = conn.getInputStream();      StringBuilder sb = new StringBuilder();      BufferedReader reader = new BufferedReader(new InputStreamReader(is));      String line;      while ((line = reader.readLine()) != null) {        sb.append(line).append("/n");      }      reader.close();      return sb.toString();    } catch (IOException e) {      Log.e(TAG, "network error for mini program ", e);      return "";    } finally {      //最后將conn斷開連接      if (conn != null) {        conn.disconnect();      }    }  }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical">  <TextView    android:id="@+id/tv_act_test"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    /></LinearLayout>
package com.example.xujiajia_sx.myexpressotest;import android.content.Intent;import android.support.test.rule.ActivityTestRule;import org.junit.Rule;import org.junit.Test;/** * Created by xujiajia_sx on 2017/8/14. */public class ActTest {  @Rule  public ActivityTestRule<ActTestActivity> mActivityTestRule=      new ActivityTestRule<ActTestActivity>(ActTestActivity.class){        @Override        protected Intent getActivityIntent() {          Intent intent=new Intent();          intent.putExtra("url","http://www.weather.com.cn/adat/sk/101310201.html");          return intent;        }        @Override        protected void beforeActivityLaunched() {          mHttpClient.setClient(new HttpURLConnectionClient());        }      };  @Test  public void mTest() throws Exception{    Thread.sleep(5000);  }}

網絡獲取不要忘記在AndroidManifest中加網絡權限喔。

這個Activity的主要邏輯就是接收Intent,然后獲取到傳過來的url,接著通過網絡獲取到url的String,顯示到TextView上。

主要測試邏輯:

首先還是要定義ActivityTestRule,確定使用哪個Activity。

與前一個例子不同的是,這里要重寫ActivityTestRule的兩個方法,getActivityIntent() 和beforeActivityLaunched()。顧名思義,一個是設置Activity獲取到的Intent,另一個是設置Activity啟動跟之前的準備工作。

筆者此處在getActivityIntent() 中設置了傳遞的url,在beforeActivityLaunched()設置的網絡獲取的方式。

有些讀者可能會好奇為什么網絡獲取的方式不默認呢,而要通過setClient()來設置?

因為這樣可以更方便我們測試,在正式的項目中,我們可能會需要在代碼中加入log等操作,但是正式的代碼一般我們是不會去修改的,但是我們可以繼承它,重寫某些方法,然后把它放到測試需要的地方。

在這里我們就可以繼承HttpURLConnectionClient 這個類,然后把繼承的子類使用setClient()來作為網絡獲取的方式。

總結

Activity的使用方法大致如此了,如果有更多需求的讀者可以查看一下官方ActivityTestRule的Reference。

鏈接如下:https://developer.android.google.cn/reference/android/support/test/rule/ActivityTestRule.html

第一種使用方法中設計到了對View的測試,由于篇幅較大,本篇文章未能詳細講述,筆者會在下篇文章做一定講解。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宣威市| 兴业县| 凤翔县| 综艺| 樟树市| 东海县| 龙南县| 长春市| 沙洋县| 进贤县| 清丰县| 平阳县| 天祝| 玛多县| 揭西县| 阿巴嘎旗| 东乡族自治县| 黄石市| 朝阳区| 高密市| 杂多县| 临沧市| 太原市| 武隆县| 离岛区| 北宁市| 郧西县| 东平县| 商丘市| 广州市| 洪湖市| 调兵山市| 阜新| 米脂县| 江北区| 大连市| 舒城县| 墨竹工卡县| 兴仁县| 大同县| 横峰县|