Android单元测试之对Activity的测试示例

 更新时间:2017年08月16日 09:10:41   作者:许佳佳233  
本篇文章主要介绍了Android单元测试之对Activity的测试示例,具有一定的参考价值,有兴趣的可以了解一下

上一篇文章已经介绍了单元测试的作用和简单示例,如果不了解的读者可以先阅读上一篇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的测试,由于篇幅较大,本篇文章未能详细讲述,笔者会在下篇文章做一定讲解。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android基于Flutter编写文件下载管理器

    Android基于Flutter编写文件下载管理器

    文件下载在很多类型的应用中会涉及,例如音乐、文档、包括图片(只是图片可以使用一些组件完成无感知的下载)。本篇介绍使用Flutter中的Dio下载方法完成文件的下载,需要的可以参考一下
    2022-03-03
  • Android使用AsyncTask下载图片并显示进度条功能

    Android使用AsyncTask下载图片并显示进度条功能

    这篇文章主要介绍了Android使用AsyncTask下载图片并显示进度条功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-02-02
  • 详解Android JNI的基本使用(CMake)

    详解Android JNI的基本使用(CMake)

    本文介绍了Android JNI的基本使用(CMake),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • Android关键字persistent详细分析

    Android关键字persistent详细分析

    这篇文章主要介绍了Android关键字persistent的相关资料,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-04-04
  • Android 相机相册权限设置方法

    Android 相机相册权限设置方法

    今天小编就为大家分享一篇Android 相机相册权限设置,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • android UI进阶之android中隐藏的layout 抽屉的使用方法

    android UI进阶之android中隐藏的layout 抽屉的使用方法

    android UI进阶之android中隐藏的layout 抽屉的使用方法,需要的朋友可以参考一下
    2013-05-05
  • Android4.X中SIM卡信息初始化过程详解

    Android4.X中SIM卡信息初始化过程详解

    这篇文章主要介绍了Android4.X中SIM卡信息初始化过程,较为详细的分析总结了Android4.X中SIM卡信息初始化的步骤及相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10
  • Android开发者必备的十个工具介绍

    Android开发者必备的十个工具介绍

    这篇文章主要介绍了Android开发者必备的十个工具介绍,在这篇文章中,讨论了10个最常见的工具,android 开发者应该了解和学习使用,需要的朋友可以参考下
    2015-03-03
  • Android指纹识别API初试

    Android指纹识别API初试

    这篇文章主要为大家详细介绍了Android指纹识别API,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Android Data Binding数据绑定详解

    Android Data Binding数据绑定详解

    本文主要介绍Android Data Binding数据绑定的知识,这里整理了详细的资料及简单示例代码帮助大家学习理解此部分知识,有需要的小伙伴可以参考下
    2016-09-09

最新评论