Android Studio中进行单元测试
以下为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中进行单元测试相关推荐
- Android Studio中进行单元测试和UI测试
目录 在Android Studio中进行单元测试和UI测试 - 1.概述 在Android Studio中进行单元测试和UI测试 - 2.创建新的Android Studio工程 在Android ...
- android studio资产目录,在Android Studio中设置单元测试的自定义资产目录
我们对使用仅需要进行单元测试的资产的Android应用程序执行单元测试是不常见的要求. 使用基于Eclipse的SDK这是简单明了的 – 测试项目是一个单独的项目,其自有资产文件夹.使用Android ...
- android studio字符串转整型,Android Studio 中的FindBugs插件使用,轻松帮你发现Bug (转)...
在日常开发过程中难免会因为一时疏忽而留下一些Bug,这些Bug就是埋在程序里的定时炸弹,如果不能及时铲除就会导致程序的不稳定,异常或闪退的现象,从而导致用户的体验的下降.那么怎么才能找出这些埋在程序里 ...
- 导入eclipse工程到Android Studio中
ref: 从 Eclipse 迁移至 Android Studio | Android Studio https://developer.android.com/studio/intro/migrat ...
- 获取Android studio 中的模拟器的界面的点的坐标(Ubuntu)
实现的方法是使用Android studio 中的hierarchyviewer 来获取. 启动模拟器 emulator 打开命令行的终端 输入 hierarchyviewer 点击 inspect ...
- android studio数据库存储数据,如何使用API 23在android studio中的数据库中存储数据?...
大多数时候我不会发布任何内容,因为我可以在其他帖子中找到我需要的所有内容,但是现在我已经有几天了,您如何在数据库中存储任何内容?这是我的Java代码如何使用API 23在android studi ...
- Android如何使用so文件和Android studio中导入so
Android中使用so文件: 做一个PDF阅读的功能,找到一个开源的库,mupdf.下载的是网上编译好的so库,导入到自己项目中的时候一直报错Java.lang.UnsatisfiedLinkErr ...
- android studio中如何设置注释模板
在开发程序的时候,我们一般都会给文件自动添加上一些关于文件的注释信息,比如开发者的名字,开发的时间,开发者的联系方式等等.那么在android studio中该如何设置呢? 工具/原料 android ...
- if函数中的android,java - 我需要帮助将IF语句方法更改为Android Studio中的SWITCH情况 - 堆栈内存溢出...
我正在Android Studio上的一个项目上工作,并且正在尝试创建过滤器. 我向其中添加了一些Checkboxes ,我想用它过滤我的RecyclerView . 使用IF语句,我可以做到,但是据 ...
- Android NDK开发之旅(2):一篇文章搞定Android Studio中使用CMake进行NDK/JNI开发
Android NDK开发之旅(2):一篇文章搞定android Studio中使用CMake进行NDK/JNI开发 (码字不易,转载请声明出处:http://blog.csdn.NET/andrex ...
最新文章
- C++——String类超详细介绍
- 华为与英特尔开展5G互操作性测试
- 网络时间协议 --- 网络对时程序
- Win10如何打开软键盘?
- 表单提交中get 和post方式的区别
- 如何理解指向指针的指针?
- 最小公倍数 [最小公约数的拓展]
- 新版chrome移动端踩坑
- 配置LVS + Keepalived高可用负载均衡集群之图文教程
- 2022年计算机二级考试C语言程序设计冲刺题及答案
- 纠错码专题——线性分组码(1)
- “关灯”看这出戏,依旧是百年至美
- 【科普】关于装机CPU参数介绍及选取原则
- python学习-第9课
- C++中用 GetModuleFileName()函数 获得程序当前的运行目录
- SAP那些事-职业篇-10-顾问最重要的思维方式
- mac os 多窗口固定顺序的方法
- k8s集成kube-prometheus
- C# 调用Windows media play 播放器方法
- tortoiseGit管理的文件没有绿色红色等图标