Reputation: 1859
I use android-annotations in my projects and I start learning unit tests. I'm trying to execute tests with robolectric but with no success (Android Studio 8.6 and Gradle). With some tutorials I configure my build.gradle and if I execute a simple test, without access any activity, the test generate OK results. But, when I try to execute tests accessing a activity, occurs errors.
Error
LoginActivityTest > testActivityAA2 FAILED
java.lang.RuntimeException at LoginActivityTest.java:33
Caused by: java.lang.reflect.InvocationTargetException at LoginActivityTest.java:33
Caused by: java.lang.IllegalStateException at LoginActivityTest.java:33
My Tests
@Config(manifest = "./src/main/AndroidManifest.xml", emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class LoginActivityTest extends InstrumentationTestCase {
//This is OK
@Test
public void testNumbers() throws Exception{
final int expected = 5;
final int reality = 5;
assertEquals(expected, reality);
}
// this occurs error
@Test
public void testActivityAA2() throws Exception {
LoginActivity_ loginnActivity = new LoginActivity_();
loginnActivity.onCreate(null);
assertNotNull(loginnActivity);
}
}
My build.gradle file
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '3.0.1'
apply plugin: 'robolectric'
buildscript {
repositories {
mavenCentral()
}
dependencies {
// replace with the current version of the Android plugin
classpath 'com.android.tools.build:gradle:0.12+'
// the latest version of the android-apt plugin
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.3'
classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
}
}
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:20.0.0'
compile 'com.android.support:appcompat-v7:20.0.0'
compile 'com.android.support:gridlayout-v7:20.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'
// Espresso
androidTestCompile files('lib/espresso-1.1.jar', 'lib/testrunner-1.1.jar', 'lib/testrunner-runtime-1.1.jar')
androidTestCompile 'com.google.guava:guava:14.0.1'
androidTestCompile 'com.squareup.dagger:dagger:1.1.0'
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
androidTestCompile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
androidTestCompile('org.robolectric:robolectric:2.3') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
exclude module: 'httpclient'
exclude module: 'maven-artifact'
exclude module: 'maven-artifact-manager'
exclude module: 'maven-error-diagnostics'
exclude module: 'maven-model'
exclude module: 'maven-project'
exclude module: 'maven-settings'
exclude module: 'plexus-container-default'
exclude module: 'plexus-interpolation'
exclude module: 'plexus-utils'
exclude module: 'wagon-file'
exclude module: 'wagon-http-lightweight'
exclude module: 'wagon-provider-api'
}
androidTestCompile 'com.squareup:fest-android:1.0.+'
}
apply plugin: 'idea'
idea {
module {
testOutputDir = file('build/test-classes/debug')
}
}
apt {
arguments {
androidManifestFile variant.processResources.manifestFile
resourcePackageName 'com.example.project'
// If you're using Android NBS flavors you should use the following line instead of hard-coded packageName
// resourcePackageName android.defaultConfig.packageName
// You can set optional annotation processing options here, like these commented options:
// logLevel 'INFO'
// logFile '/var/log/aa.log'
}
}
robolectric {
include '**/*Test.class'
exclude '**/espresso/**/*.class'
}
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
defaultConfig {
applicationId "br.com.nexxcity.nexxcitypos"
minSdkVersion 7
targetSdkVersion 19
versionCode 1
versionName "1.0"
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
androidTest {
setRoot('src/androidTest')
}
}
}
Can you help me guys ? Thx for patience
Upvotes: 0
Views: 1678
Reputation: 5151
You shouldn't initialise the Activity like that for Robolectric tests. Use Robolectric.buildActivity() (documentation: http://robolectric.org/activity-lifecycle/)
For example: Robolectric.buildActivity(LoginActivity.class).create().get()
Depending on what you need to test, you can chain additional methods as well like visible(), start() etc.
Upvotes: 1