Kel*_*dos 4 tdd android junit4 robolectric
我是Android开发人员和TDD热衷.最近,我了解了一个新的测试框架Robolectric,它似乎远远优于Android Studio默认提供的JUnit 3解决方案.我想设置它,但经过多次尝试,失败后java.lang.RuntimeException: Stub!我什么也没做到.
这是我的问题:
如何在Andoird Studio 1.0上逐步设置Robolectric测试框架?请注意:
此框架的设置非常有问题,并且在不同版本的Android Studio和IntelliJ之间存在许多问题.我浏览了所有这些,但无济于事.我需要在Android Studio 1.0上成功使用该框架的人的帮助.
编辑:
在那几个月Robolectric了解Android Studio建立的过程,提高了不少,所以尝试以下,"长"的方式之前,只要尝试官方指南在这里!:-)
好的,我设法安装了它!它非常棘手,并且与最新的Gradle 1.0.0不完全兼容,但它的工作就像一个魅力!
我的解决方案主要基于本教程
因此,您的build.gradle文件(项目文件夹中的"内部"文件)应如下所示:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.1'
classpath 'org.robolectric:robolectric-gradle-plugin:0.14.0'
}
}
allprojects {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
}
}
apply plugin: 'com.android.application'
apply plugin: 'robolectric'
android {
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
applicationId '[your app id]'
minSdkVersion 9
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
sourceSets {
androidTest.setRoot('src/androidTest') // This one is important, make sure to avoid typos in it, or you will get empty tests
}
lintOptions {
abortOnError false
disable 'InvalidPackage'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.2'
// ================== TESTING LIBRARIES ======================
androidTestCompile 'junit:junit:4.10'
androidTestCompile 'org.robolectric:robolectric:2.4'
androidTestCompile 'org.bouncycastle:bcprov-jdk15on:1.50'
}
robolectric {
// configure the set of classes for JUnit tests
include '**/*Test.class' //Make sure you call all your test classes according to this expression!!!
// configure max heap size of the test JVM
maxHeapSize = "2048m"
}
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.1.201405082137"
}
def coverageSourceDirs = [
'../app/src/main/java'
]
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories = fileTree(
dir: '../app/build/intermediates/classes/debug',
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files('../app/build/jacoco/testDebug.exec')
reports {
xml.enabled = true
html.enabled = true
}
}
Run Code Online (Sandbox Code Playgroud)
它看起来很像"外部"build.gradle文件,但所有这些东西都必须在这里.
我在这里不需要一些元素,但我尽力将其削减到核心部分.在这之后,创建一个Test类,如下所示:
import android.app.Activity;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
@Config(emulateSdk = 18, reportSdk = 18) //Those are required as the framework does not yet support higher API levels
@RunWith(RobolectricTestRunner.class)
public class RoboDummyTest {
@Test
public void testActivityFound() {
Activity activity = Robolectric.buildActivity(MainActivity.class).create().get();
Assert.assertNotNull(activity);
}
}
Run Code Online (Sandbox Code Playgroud)
所有数据当然是存根:-)
最后,通过gradlew test在Android Studio中输入终端来运行测试.结果将保存到build项目目录中的html文件中.
我希望这对任何计划使用Android Studio 1.0设置Robolectric的人都有帮助.快乐TDDing!
编辑:我曾经写过答案的博客文章的作者Kvandermast,提供以下链接到包含工作示例的存储库,更新每个Android Studio更新:https: //github.com/kvandermast/我-robolectric-应用
| 归档时间: |
|
| 查看次数: |
3540 次 |
| 最近记录: |