Writing Your First Test
Let's say you have an activity layout that represents a welcome screen:
<?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"> <Button
android:id="@+id/login"
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </LinearLayout>
We want to write a test that asserts that when a user clicks on a button, the app launches the LoginActivity.
//我们想要测试的是当用户点击了button,应用是否启动了LoginActivity
public class WelcomeActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_activity); final View button = findViewById(R.id.login);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
}
});
}
}
In order to test this, we can check that when a user clicks on the "Login" button, we start the correct intent. Because Robolectric is a unit testing framework, the LoginActivity will not actually be started, but we can check that the WelcomeActivity fired the correct intent:
//为了测试这一点,我能够检查当用户点击了button时我们开启了正确的intent,因为robolectric是单元测试框架,LoginActivity并没有真的被启动,但是我们能够检查WelcomeActivity是否开启了正确的intent
@RunWith(RobolectricTestRunner.class)
public class WelcomeActivityTest { @Test
public void clickingLogin_shouldStartLoginActivity() {
WelcomeActivity activity = Robolectric.setupActivity(WelcomeActivity.class);
activity.findViewById(R.id.login).performClick(); Intent expectedIntent = new Intent(activity, LoginActivity.class);
assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(expectedIntent);
}
}
Writing Your First Test的更多相关文章
- Spring Enable annotation – writing a custom Enable annotation
原文地址:https://www.javacodegeeks.com/2015/04/spring-enable-annotation-writing-a-custom-enable-annotati ...
- Writing to a MySQL database from SSIS
Writing to a MySQL database from SSIS 出处 : http://blogs.msdn.com/b/mattm/archive/2009/01/07/writin ...
- Writing Clean Code 读后感
最近花了一些时间看了这本书,书名是 <Writing Clean Code ── Microsoft Techniques for Developing Bug-free C Programs& ...
- JMeter遇到的问题一:Error writing to server(转)
Java.io.IOException: Error writing to server异常:我测试500个并发时,系统没有问题:可当我把线程数加到800时,就出现错误了,在"查看结果树&q ...
- java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException
问题描述: 严重: IOException while loading persisted sessions: java.io.WriteAbortedException: writing abort ...
- Markdown syntax guide and writing on MWeb
Philosophy Markdown is intended to be as easy-to-read and easy-to-write as is feasible.Readability, ...
- 《Writing Idiomatic Python》前两部分的中文翻译
汇总了一下这本小书前两部分的内容: 翻译<Writing Idiomatic Python>(一):if语句.for循环 翻译<Writing Idiomatic Python> ...
- 翻译《Writing Idiomatic Python》(五):类、上下文管理器、生成器
原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...
- 翻译《Writing Idiomatic Python》(四):字典、集合、元组
原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...
- 翻译《Writing Idiomatic Python》(三):变量、字符串、列表
原书参考:http://www.jeffknupp.com/blog/2012/10/04/writing-idiomatic-python/ 上一篇:翻译<Writing Idiomatic ...
随机推荐
- 消息队列msmq
http://q.cnblogs.com/q/26895/ 远程队列必须现在运程服务器上创建. 在 Windows Server 2008 上安装 IIS 服务和 MSMQ 功能后,系统会在 IIS ...
- Dataguard配置前提条件
Data Guard配置前提条件 配置Data Guard必须保证以下前提条件: 1.Data Guard是Oracle企业版的组件.Oracle标准版里没有这个控件.所以Data Guard配置所使 ...
- 移植qt5.3.1到arm
最近刚入手一款开发板(比较低端,刚毕业工作还没一个月,穷屌丝一个).在学校以前都是用的是Friendly Arm的6410,使用的qt版本是使用的最多的4.7.0版本,入手的板子是4.7.4.qt5出 ...
- Android开源项目发现--- 工具类数据库ORM篇(持续更新)
orm的db工具类,简化建表.查询.更新.插入.事务.索引的操作 1. greenDAO Android Sqlite orm的db工具类 项目地址:https://github.com/greenr ...
- 【POJ】3630 Phone List
静态字典树. #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 10005 ...
- Solr 多核(MultiCore)配置
Solr Multicore意义 Solr Multicore 是 solr 1.3 的新特性.其目的一个solr实例,可以有多个搜索应用.< xmlnamespace prefi ...
- 使用Redis作为消息队列服务场景应用案例
一.消息队列场景简介 "消息"是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更复杂,可能包含嵌入对象.消息被发送到队列中,"消息队列&qu ...
- 【转】VMware 11安装Mac OS X 10.10 及安装Mac Vmware Tools.
原文网址:http://www.cnblogs.com/Anand/p/4483727.html 先上一张效果图兴奋一下,博主穷屌丝一个,只能通过虚拟黑苹果体验下高富帅的生活,感觉超爽的,废话不多说的 ...
- 贪心(哈夫曼树):HDU 5884 sort
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA2QAAAKACAIAAAB8KCy/AAAgAElEQVR4nOy9a5Adx3UmWL+kHxuekU ...
- 博弈论(男人八题):POJ 1740 A New Stone Game
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5694 Accepted: 3119 ...