Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Roboelectric and Android Studio

I've been trying to do it for a few days, with no result. I need to set up Robolectric in Android Studio (0.8.9, latest version). I followed different tutorials Android Unit and Integration testing, Roboelectric installation for Unit testing, Android Gradle app with Roboelectric, How to run Roboelectric JUnit tests but always got some kind of error.

So I created module specially for testing:

enter image description here

enter image description here

Kedzoh (Project) build.gradle:

// Top-level build file where you can add configuration options common to all sub-    projects/modules.
buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:0.12.+'
    classpath 'org.robolectric:robolectric-gradle-plugin:0.12.+'
}
}

allprojects {
repositories {
    jcenter()
}
}

app build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'android'

android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
    applicationId 'com.dev.kedzoh'
    minSdkVersion 9
    targetSdkVersion 19
    versionCode 14
    versionName '1.6.7'
}
buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
productFlavors {
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.android.support:appcompat-v7:19.+'
compile 'com.google.android.gms:play-services:4.2.42'
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.picasso:picasso:2.3.3'
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.android.support:support-v4:19.+'
compile 'com.google.guava:guava:18.0-rc1'
compile 'com.sothree.slidinguppanel:library:+'
compile 'com.larswerkman:HoloColorPicker:1.4'
}

kedzoh-tests build.gradle:

apply plugin: 'java'

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.squareup.picasso:picasso:2.3.3'
// You must install or update the Support Repository through the SDK manager to use this dependency.
compile 'com.google.guava:guava:18.0-rc1'
compile 'com.sothree.slidinguppanel:library:+'
compile 'com.larswerkman:HoloColorPicker:1.4'

testCompile 'junit:junit:4.+'
testCompile 'org.robolectric:robolectric:2.2'
}

At this point I cannot import Robolectric classes, it gives me error. When I add apply plugin: 'robolectric' to kedzoh-tests build.gradle, it asks for 'android' plugin. After I add it, it complains there is no Manifest and fails to build. I'm not sure that this is the right configuration, since it never worked really. Could anyone give some advice how to set Robolectric in Android Studio, please?

EDIT:

I tried the answer below, but still stuck with "Class not found" error:

enter image description here

enter image description here

enter image description here

like image 313
Roman Avatar asked Oct 21 '14 10:10

Roman


People also ask

What is Robolectric used for?

Robolectric provides a JVM compile version of the android. jar file. Robolectric handles views, resource loading, and many other things that are implemented in the Android native. This enables you to run your Android tests in your development environment, without requiring any other setup to run the test.

Is Robolectric deprecated?

setupActivity() is deprecated in Android unit test. Save this question.

What are the disadvantages of using Robolectric?

So the main benefit about Robolectric is that it is a lot faster than Espresso or Instrumented tests in general. The downside is that it fakes an Android environment which you should be aware of. To validate real world problems, better use a classic Android Framework approach.

What is the difference between Mockito and Robolectric?

Mockito is used for mocking the dependency which means if you want to access an real object in test environment then you need to fake it or we can say mock it. Now a days it is very easier to do mocking of the objects with Mockito. Roboelectric is the industry-standard unit testing framework for Android.


1 Answers

There already different project templates for robolectric. Did you try https://github.com/nenick/android-gradle-template ?

like image 62
nenick Avatar answered Oct 06 '22 11:10

nenick