今天学习了单元测试框架,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. JavaMail 的简单使用

    概述 邮件功能模块在大多数网站中,都是必不可少的功能模块.无论是用户注册还是重置密码,邮件都是比较常用的一个方式.本文主要介绍 JavaMail 的简单使用,方便大家快速开发,供大家参考.完整的 de ...

  2. 如何在SecureCRT中给linux上传和下载文件

    方法/步骤     需要上传或者下载,需要使用rz和sz命令.如果linux上没有这两个命令工具,则需要先安装.可以使用yum安装.运行命令yum install lrzsz.   安装完成后就可以使 ...

  3. 安装纯净 ubuntu linux (非虚拟机)

    //--------------- Chinese version --------------------------------------------------// 前提条件:有另一台电脑(w ...

  4. sap.ui.require in SAP UI5 and require in nodejs

    UI5 例如我需要在controller的onShowHello里通过MessageToast弹一个消息显示在UI上, 我需要先定义我自己的controller,该controller extend自 ...

  5. 屏蔽firefox浏览器连接失败页面的广告

    现象 最近一直在使用firefox浏览器(版本:57.0.1(64位)),同步书签特别方便,但是最近发现当访问的一个不存在的网址时,连接失败页面竟然有广告!firefox不是号称没有广告吗? 分析 F ...

  6. ABI与编译器:ABI是由内核和工具链定义和实现的

    http://book.51cto.com/art/201412/460857.htm <Linux系统编程(第2版)>第1章入门和基本概念,这一章着眼于Linux系统编程的基础概念并从程 ...

  7. 【洛谷P3390】矩阵快速幂

    矩阵快速幂 题目描述 矩阵乘法: A[n*m]*B[m*k]=C[n*k]; C[i][j]=sum(A[i][1~n]+B[1~n][j]) 为了便于赋值和定义,我们定义一个结构体储存矩阵: str ...

  8. 安装ubuntu-tweak

    第一步:添加tweak源 sudo add-apt-repository ppa:tualatrix/ppa   第二步:更新 sudo apt-get update   第三步:安装ubuntu-t ...

  9. apache以天为单位生成日志

    编辑/etc/httpd/conf.d/vhost.conf,修改ErrorLog和CustomLog: ErrorLog "|rotatelogs /var/log/httpd/phpdd ...

  10. 通过eclipse启动tomcat设置JAVA_OPTS失败的解决方案

    clipse中配置tomcat方法: Window-->Preference-->Server-->Runtime Environment-->add-->Apache ...