记录一下,使用的 Android Studio 版本是 0.8.2 (Beta)
在 Project 目录下的 build.gralde 文件中的 dependencies 添加二行
classpath 'org.robolectric:robolectric-gradle-plugin:0.+' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+'
引入这二个插件 robolectric-gradle-plugin,android-apt
之后在 Module 目录下的 build.gralde 文件
apply plugin: 'com.android.application' apply plugin: 'robolectric' apply plugin: 'android-apt' android { compileSdkVersion 19 buildToolsVersion "19.1.0" defaultConfig { applicationId "cn.platoon.demo" minSdkVersion 8 targetSdkVersion 19 versionCode 1 versionName "1.0" } buildTypes { release { runProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:19.+' apt "org.androidannotations:androidannotations:3.0.+" compile "org.androidannotations:androidannotations-api:3.0.+" androidTestCompile 'junit:junit:4.+' androidTestCompile ('org.robolectric:robolectric:2.+') androidTestCompile 'com.squareup:fest-android:1.+' } apt { arguments { androidManifestFile variant.processResources.manifestFile resourcePackageName 'cn.platoon.demo' } } robolectric { include "**/*Test.class" }
配置中的包名改为自己的包名。
现在开始写 MainActivity 直接上代码
@EActivity(R.layout.activity_main) @OptionsMenu(R.menu.main) public class MainActivity extends ActionBarActivity { @ViewById(R.id.text) TextView text; @OptionsItem(R.id.action_settings) public void action_settings() { text.setText("action_settings"); } @Click(R.id.button) public void button() { text.setText("button"); } }
Layout 文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" > <TextView android:id="@+id/text" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:text="测试" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
Menu 文件
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"> <item android:id="@+id/action_settings" android:title="@string/action_settings" android:orderInCategory="100" app:showAsAction="never" /> </menu>
之后就是测试文件,新版本的会自己建一个 androidTest 目录,测试文件就放在这下面
@RunWith(RobolectricTestRunner.class) @Config(emulateSdk = 18) public class MainActivityTest { Activity activity; @Before public void before() { activity = Robolectric .buildActivity(MainActivity_.class) .create().get(); } private final MenuItem actionSettingsMenuItem = new TestMenuItem() { @Override public int getItemId() { return R.id.action_settings; } }; @Test public void testActionSettings() throws Exception { TextView text = (TextView) activity.findViewById(R.id.text); activity.onOptionsItemSelected(actionSettingsMenuItem); assertThat(text.getText().toString(), equalTo("action_settings")); } @Test public void testButtonClick() throws Exception { Button button = (Button) activity.findViewById(R.id.button); TextView text = (TextView) activity.findViewById(R.id.text); button.performClick(); assertThat(text.getText().toString(), equalTo("button")); } }
现在文件都全了,可以在 Gralde task 里面找到 test 这个 task,直接运行就可以了。
完成后看不到什么提示,要到Module目录文件下的 build\test-report\index.html 这个文件,打开就是测试报告。
要注意的是,现在下的 robolectric 是 2.3 的版本,只能测试到 sdk 18,所以要在测试类上加 @Config(emulateSdk = 18)。
我也是刚刚开始研究 android,希望和大家分享一下。
相关推荐
这个资源是关于在 Android Studio 中设计的转换器应用程序,用来转换米和英尺之间的单位。通常情况下,我们可能需要在不同的测量标准之间进行转换,因此该资源对于需要进行度量转换的用户具有很高的实用价值。 这个...
AndroidStudio+SDK
linux版本androidStudio+sdk
本代码包括客户端和服务端两部分,客户端实现用户的注册和登录,并发送信息至服务端;服务端通过MySQL数据库对接收到的注册信息进行增删改等,实现一个完整的交互过程。
android studio+jdk 64位配置下环境变量就可以用
android studio+python鲜花识别代码
python毕业设计-基于Android Studio+Django的校园论坛设计与实现+使用说明.zippython毕业设计-基于Android Studio+Django的校园论坛设计与实现+使用说明.zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能...
毕业设计,基于SpringBoot+AndroidStudio+百度云平台人脸识别开发的学生考勤签到系统,含完整源码、数据库脚本 基于AndroidStudio百度云平台人脸识别学生考勤签到系统设计 开发软件:Idea + Mysql + AndroidStudio...
Android ViewPager+RadioGroup+Fragment超高仿微信底部滑动菜单AndroidStudio源码 Blog:http://blog.csdn.net/shenyuanqing/article/details/46670761
Androidstudio+ffmpeg+camke播放器 编译的ffmpeg成so库使用cmake加载so及cpp,具有代码自动补全,一键编译java cpp功能的 ffmpeg Android平台播放器源码
用法从 Android Studio 导入项目(不要打开它)。 请注意,测试不会通过 IDE 运行。 这个小应用程序可以通过通常的方式编译和安装。 运行 POJO 和 UI 测试: ./gradlew unitTest # POJO./gradlew test # UI使用的...
从 Android Studio 导入项目(不要打开它)。 请注意,测试不会通过 IDE 运行。 这个小应用程序可以通过通常的方式编译和安装。 运行 POJO 和 UI 测试: ./gradlew unitTest # POJO ./gradlew test # UI 使用的...
1、Android studio+ASPnet信息安全技术应用在票房数据透明性中的,描述: 本文采用DES加密算法来保证票房数据的安全。本文的主要工作是研究信息安全技术在票房数据透明性中的应用”的设计与实现,设计技术采用...
适合刚入门Android Studio的伙伴,详细的操作教程和使用Gradle构建项目.
运行Demo前请先安装 OpenCV Manager (必须!!) 并给Demo授予CAMERA权限
androidstudio+夜神模拟器的安装,教程很详细,比从网上找的一般的好的多了。。
Android Studio项目+报告+app文件
android studio+myeclipse+mysql自定义控件下拉框的数据绑定,喜欢的可以下载。
android社交聊天APP完整源码(服务端+客户端),使用Android Studio开发,包含android客户端源码、php+mysql服务端源码,功能完整,带附近用户和好友系统,支持即时消息和图像发送,支持表情符号和消息推送功能,...
若需要完整Android Studio代码,请联系博主。 内容概要:该资源是一个基于Android Studio的日记App的移动平台软件设计课程设计,学生将学习如何使用Android Studio进行移动应用开发,并通过设计和实现一个日记App来...