今天学习了单元测试框架,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. HCNA配置telnet远程管理

    1.拓扑图 说明:通过配置最终能通过R5 用telnet协议登陆到R4上并将R4改名为R44 2.R4配置 <Huawei>sys Enter system view, return us ...

  2. UOJ #207. 共价大爷游长沙

    #207. 共价大爷游长沙 链接:http://uoj.ac/problem/207 题意:给一棵树,要求支持加边.删边.询问一条边是否被所有路径覆盖.同时路径端点集合有加入与删除操作. 想法: 考虑 ...

  3. Ubuntu 如何将桌面上的Home中的文件夹除去

    安装Ubuntu后, 由于无法用Terminal(终端)进入带中文的文件夹,会引起很多操作不便.很多朋友想到了将它们都改成中文,但是当再次开机重启使却会发现,原本光洁的桌面现在竟然出现了一堆文件夹?? ...

  4. *204. Count Primes (siecing prime)

    Count the number of prime numbers less than a non-negative number, n. Example: Input: 10 Output: 4 E ...

  5. POJ-2029 Get Many Persimmon Trees---二维树状数组+枚举

    题目链接: https://vjudge.net/problem/POJ-2029 题目大意: 有N棵树在一个n*m的田里,给出每颗树的坐标 用一个s*t的矩形去围,最多能围几棵树 思路: 用二维树状 ...

  6. 2017.10.18 微机原理与接口----汇编语言语法和DOS功能调用

    4.1 汇编语言中的基本数据 ·标识符 ·常数 ·变量具有三个属性: (1)段地址(SEG):变量所在段的段地址 (2)偏移地址(OFFSET):变量所在段内的偏移地址 (3)类型(TYPE):每个变 ...

  7. python_2_变量的使用2

    ''' 多行注释(三个单引号,或者双引号) gf_of_oldboy="Chen rong hua"#变量的表示办法1,用下划线(老男孩的女朋友) GfOfOldboy=" ...

  8. ImportError: No module named images

    [问题] 在使用学习wxPython时,一个Dem抱有如题所示错误 [解决] images 只不过是wxpython自带demo中的一个文件 体验wxpython IN action的时候Import ...

  9. 实际案例告诉你为什么Oracle不建议使用varchar2来存时间数据

    问题现象2015年9月客户系统中一条高逻辑读的SQL语句,在业务高峰期执行频率较高,导致系统逻辑读居高不下,同时带高了系统CPU,SQL语句主体部分如下 SELECT /* ^^*/ COUNT(DI ...

  10. Linux文件服务器实战(系统用户)

    ftp匿名用户设置完成之后任何人都可以访问服务器端文件,目录,甚至可以修改删除文件和目录,,那如何存放私密文件并保证文件或者目录专属于拥有者呢,就需要使用vsftp系统用户来实现了. 1.在linux ...