以下为activity_mian代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.gxd.hellowordhw.MainActivity"    android:orientation="vertical">

    <TextView        android:id="@+id/textview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hello World!" />

    <EditText

        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/editText" />

    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="SAY HELLO!"        android:id="@+id/button"        android:layout_gravity="center_horizontal" />

</LinearLayout>

以下为Mainactivity代码:
package com.example.gxd.hellowordhw;

import android.support.v7.app.AppCompatActivity;        import android.os.Bundle;        import android.view.View;        import android.widget.EditText;        import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }

    public void sayHello(View view){        TextView textView=(TextView)findViewById(R.id.textview);        EditText editText=(EditText)findViewById(R.id.editText);        textView.setText("Hello,"+editText.getText().toString()+"!");    }}
以下为MainactivityText代码:public class MainActivityTest {     private static final String STRING_TO_BE_TYPEDTO = "Peter";    @Rule    public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

    @Test    public void testSayHello() throws Exception {

        onView(withId(R.id.editText)).perform(TestRule(STRING_TO_BE_TYPEDTO),closeSoftKeyboard);        onView(withId(R.id.button)).perform(click());        String expectedText="Hello,"+ STRING_TO_BE_TYPEDTO +"!";        onView(withId(R.id.textview)).check(matches(withText(expectedText)));

    }

}

以下为build.gradle代码:
apply plugin: 'com.android.application'

android {    compileSdkVersion 23    buildToolsVersion "24.0.2"

    defaultConfig {        applicationId "com.example.gxd.hellowordhw"        minSdkVersion 19        targetSdkVersion 23        versionCode 1        versionName "1.0"        //add for test        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }}

dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    testCompile 'junit:junit:4.12'    compile 'com.android.support:appcompat-v7:23.4.0'    //add for test    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    }}布局:

转载于:https://www.cnblogs.com/gudaping/p/6641022.html

Android Studio中进行单元测试相关推荐

  1. Android Studio中进行单元测试和UI测试

    目录 在Android Studio中进行单元测试和UI测试 - 1.概述 在Android Studio中进行单元测试和UI测试 - 2.创建新的Android Studio工程 在Android ...

  2. android studio资产目录,在Android Studio中设置单元测试的自定义资产目录

    我们对使用仅需要进行单元测试的资产的Android应用程序执行单元测试是不常见的要求. 使用基于Eclipse的SDK这是简单明了的 – 测试项目是一个单独的项目,其自有资产文件夹.使用Android ...

  3. android studio字符串转整型,Android Studio 中的FindBugs插件使用,轻松帮你发现Bug (转)...

    在日常开发过程中难免会因为一时疏忽而留下一些Bug,这些Bug就是埋在程序里的定时炸弹,如果不能及时铲除就会导致程序的不稳定,异常或闪退的现象,从而导致用户的体验的下降.那么怎么才能找出这些埋在程序里 ...

  4. 导入eclipse工程到Android Studio中

    ref: 从 Eclipse 迁移至 Android Studio | Android Studio https://developer.android.com/studio/intro/migrat ...

  5. 获取Android studio 中的模拟器的界面的点的坐标(Ubuntu)

    实现的方法是使用Android studio 中的hierarchyviewer 来获取. 启动模拟器 emulator 打开命令行的终端 输入 hierarchyviewer 点击 inspect ...

  6. android studio数据库存储数据,如何使用API​​ 23在android studio中的数据库中存储数据?...

    大多数时候我不会发布任何内容,因为我可以在其他帖子中找到我需要的所有内容,但是现在我已经有几天了,您如何在数据库中存储任何内容?这是我的Java代码如何使用API​​ 23在android studi ...

  7. Android如何使用so文件和Android studio中导入so

    Android中使用so文件: 做一个PDF阅读的功能,找到一个开源的库,mupdf.下载的是网上编译好的so库,导入到自己项目中的时候一直报错Java.lang.UnsatisfiedLinkErr ...

  8. android studio中如何设置注释模板

    在开发程序的时候,我们一般都会给文件自动添加上一些关于文件的注释信息,比如开发者的名字,开发的时间,开发者的联系方式等等.那么在android studio中该如何设置呢? 工具/原料 android ...

  9. if函数中的android,java - 我需要帮助将IF语句方法更改为Android Studio中的SWITCH情况 - 堆栈内存溢出...

    我正在Android Studio上的一个项目上工作,并且正在尝试创建过滤器. 我向其中添加了一些Checkboxes ,我想用它过滤我的RecyclerView . 使用IF语句,我可以做到,但是据 ...

  10. Android NDK开发之旅(2):一篇文章搞定Android Studio中使用CMake进行NDK/JNI开发

    Android NDK开发之旅(2):一篇文章搞定android Studio中使用CMake进行NDK/JNI开发 (码字不易,转载请声明出处:http://blog.csdn.NET/andrex ...

最新文章

  1. C++——String类超详细介绍
  2. 华为与英特尔开展5G互操作性测试
  3. 网络时间协议 --- 网络对时程序
  4. Win10如何打开软键盘?
  5. 表单提交中get 和post方式的区别
  6. 如何理解指向指针的指针?
  7. 最小公倍数 [最小公约数的拓展]
  8. 新版chrome移动端踩坑
  9. 配置LVS + Keepalived高可用负载均衡集群之图文教程
  10. 2022年计算机二级考试C语言程序设计冲刺题及答案
  11. 纠错码专题——线性分组码(1)
  12. “关灯”看这出戏,依旧是百年至美
  13. 【科普】关于装机CPU参数介绍及选取原则
  14. python学习-第9课
  15. C++中用 GetModuleFileName()函数 获得程序当前的运行目录
  16. SAP那些事-职业篇-10-顾问最重要的思维方式
  17. mac os 多窗口固定顺序的方法
  18. k8s集成kube-prometheus
  19. C# 调用Windows media play 播放器方法
  20. tortoiseGit管理的文件没有绿色红色等图标

热门文章

  1. 十六、Java四种内部类详解、匿名对象的使用
  2. ES(Elasticsearch)更改字段索引
  3. 为项目编写Readme.MD文件
  4. PowerDesigner 16.5 安装图解 与 快速入门
  5. android使用地图编程,基于Android实现百度地图定位过程详解
  6. PrintService类打印
  7. Python学习 Day 039 - HTML
  8. MemCache在Windows下环境的搭建及启动
  9. My SQL中show命令--MySQL中帮助查看
  10. CSS3 基础(1)——选择器详解