今天学习了单元测试框架,Robolectric。初步感觉,可能我测试的少,没有感觉Robolectric能有多大的帮助。虽然可以帮助创建activity。可以模拟点击事件。可是有什么呢。

好吧,讲下使用:

参看:

http://robolectric.org/extending/

https://github.com/robolectric/robolectric

1.这个框架是要配合junit使用的。所以:

    testCompile 'junit:junit:4.12'
testCompile "org.robolectric:robolectric:3.1"

2.编写测试代码:

package cebactivity.chinaceb.com.myapplication;

import android.content.Intent;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity; import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals; /**
* Created by Administrator on 2016/6/26.
*/
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 19)
public class testmain {
@Test
public void testMainActivity() {
MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);
mainActivity.findViewById(R.id.bt).performClick(); Intent expectedIntent = new Intent(mainActivity, SecondActivity.class);
ShadowActivity shadowActivity = Shadows.shadowOf(mainActivity);
Intent actualIntent = shadowActivity.getNextStartedActivity();
assertEquals(expectedIntent, actualIntent);
} @Test
public void testActivity() {
MainActivity sampleActivity = Robolectric.setupActivity(MainActivity.class);
assertNotNull(sampleActivity);
assertEquals(sampleActivity.getClass().getName(), "cebactivity.chinaceb.com.myapplication.MainActivity");
}
}

第一个测试老是通不过,妈的,明明是同一个,偏偏不让我过。好吧,不许吐槽。

Robolectric的更多相关文章

  1. Android 单元测试(junit、mockito、robolectric)

    1.运用JUnit4 进行单元测试 首先在工程的 src 文件夹内创建 test 和 test/java 文件夹. 打开工程的 build.gradle(Module:app)文件,添加JUnit4依 ...

  2. Android studio下gradle Robolectric单元测试配置

    android studio下gradle Robolectric单元测试配置 1.Robolectric Robolectric是一个基于junit之上的单元测试框架.它并不依赖于Android提供 ...

  3. Robolectric 配置

    费了些工夫,已配好,按记录留记录 按官网操作http://robolectric.org/getting-started/ 1引包 testCompile "org.robolectric: ...

  4. 在Android Studio中用Gradle添加Robolectric

    我们用Robolectric测试的话需要在gradle中进行配置,国内的详细教程太过简易,而且很多是低版本下的配置方案.所以经过本人的仔细摸索,找到了现在高版本中的配置方案,主要还是参考了官网的配置教 ...

  5. robolectric环境的搭建

    最近在学习测试驱动开发(Test-Driven Development),测试驱动开始是极限编程的一种方式,提倡在真正编写代码之前先根据需求编写测试代码(当然这个测试代码是不可能通过的),然后根据测试 ...

  6. Extending Robolectric

    Robolectric is a work in progress, and we welcome contributions from the community. We encourage dev ...

  7. Configuring Robolectric

    There are numerous ways to customize how Robolectric behaves at runtime. Config Annotation The prima ...

  8. Robolectric Test-Drive Your Android Code

    Running tests on an Android emulator or device is slow! Building, deploying, and launching the app o ...

  9. Robolectric 探索之路

    layout: post title: Roboletric探索之路,从抗拒到依赖 description: Roboletric Android Unit Testing category: blo ...

  10. Robolectric使用教程

    转载请标明出处:http://blog.csdn.net/shensky711/article/details/53561172 本文出自: [HansChen的博客] 概述 怎样使用 为项目加入依赖 ...

随机推荐

  1. 青石B2C商城

    平台: Windows 类型: 虚拟机镜像 软件包: azure commercial ecbluestone ecommerce ecommerce solution 服务优惠价: 按服务商许可协议 ...

  2. Azure 3月新公布(二)

    Azure 3月新发布:HDInsight 的 Apache Hadoop 以及 ExpressRoute 超高性能网关层正式发布,SQL Database Premium RS 层发布公共预览版 A ...

  3. Arcgis for Js之鼠标经过显示对象名的实现

    在浏览地图时,移动鼠标经过某个对象或者POI的时候,能够提示该对象的名称对用户来说是很实用的,本文讲述在Arcgis for Js中,用两种不同的方式来实现该效果. 为了有个直观的概念,先给大家看看实 ...

  4. An internal error occurred during: "Generating JAX-WS Web Services".Unable to create JAXBContext

    使用myeclipse10 创建webservice时,提示错误: An internal error occurred during: "Generating JAX-WS Web Ser ...

  5. struts2表单提交Date数据无法接收

    问题:在Struts2环境下,提交含有Date类型数据表单,但是在action中没有接收到:String就可以直接接收到: --网络搜索后,说Struts2可以自己转,但是目前没发现有: 然后在狂搜, ...

  6. MySQL入门很简单: 5 索引

    1. 索引的含义和特点 索引:创建在表上,是对数据库表中一列或多列的值进行排序的一种结构. 存储类型: B性树(BTREE)索引和哈希(HASH)索引: InnoDB和MyISAM支持BTREE索引, ...

  7. 你真的会用ABAP, Java和JavaScript里的constructor么?

    如果constructor里调用了一个成员方法,这个方法被子类override了,当初始化一个子类实例时,父类的构造函数被的调用,此时父类构造函数的上下文里调用的成员方法,是父类的实现还是子类的实现? ...

  8. Java nio socket与as3 socket(粘包解码)连接的应用实例

    对Java nio socket与as3 socket连接的简单应用 <ignore_js_op>Java nio socket与as3 socket连接的应用实例.rar (9.61 K ...

  9. Pj Immediate Decodability

    判断一个串是否是其他的前缀 我们需要建立一颗tire树 在插入边的时候,如果遇到一个其他串的结尾,那么就说明至少有一个串,是插入串的前缀.如果在插入完后没有新增的节点,那么插入的串就是其他串的前缀 # ...

  10. 动态生成的DOM做点击事件无效

    有时候我们的标签都是从后台获取的数据,然后利用JS添加到页面上,当我们写生成的标签的点击事件(click)时没有效果. 例如: <section> 测试动态生成的DOM点击事件 <b ...