Thursday, 8 August 2013

Robolectric with Gradle: package org.robolectric does not exist

Robolectric with Gradle: package org.robolectric does not exist

I am trying to run robolectric tests with gradle android build system. I
have followed the instructions given here to try to make it work but while
running tests using gradle robolectric I am badly stuck at the following
error -
/Users/Sreekanth/Documents/artoo/Code/Android/HelloWorld/src/test/java/com/example/helloworld/MyTest.java:8:
package org.robolectric does not exist
import org.robolectric.RobolectricTestRunner;
My Project has the following structure -
.
„¥„Ÿ„Ÿ AndroidManifest.xml
„¥„Ÿ„Ÿ assets
„¥„Ÿ„Ÿ bin
„¥„Ÿ„Ÿ build.gradle
„¥„Ÿ„Ÿ gen
„¥„Ÿ„Ÿ libs
„¥„Ÿ„Ÿ res
„¤„Ÿ„Ÿ src
„¥„Ÿ„Ÿ main
„  „¤„Ÿ„Ÿ java
„  „¤„Ÿ„Ÿ com
„  „¤„Ÿ„Ÿ example
„  „¤„Ÿ„Ÿ helloworld
„  „¥„Ÿ„Ÿ MainActivity.java
„¤„Ÿ„Ÿ test
„¤„Ÿ„Ÿ java
„¤„Ÿ„Ÿ com
„¤„Ÿ„Ÿ example
„¤„Ÿ„Ÿ helloworld
„¤„Ÿ„Ÿ MyTest.java
Here is my build.gradle:
import com.android.build.gradle.api.TestVariant
buildscript {
repositories {
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
classpath 'com.novoda.gradle:robolectric-plugin:0.0.1-SNAPSHOT'
}
}
apply plugin: 'android'
apply plugin: 'robolectric'
apply plugin: 'maven'
dependencies {
compile 'com.android.support:support-v4:13.0.+'
robolectricCompile 'org.robolectric:robolectric:2.0'
robolectricCompile group: 'junit', name: 'junit', version: '4.11'
}
repositories {
mavenLocal()
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
defaultConfig {
minSdkVersion 7
targetSdkVersion 18
}
}
My MyTest.java file:
package com.example.helloworld;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class MyTest {
@Test
public void shouldHaveHappySmiles() throws Exception {
String appName = new
MainActivity().getResources().getString(R.string.app_name);
assertThat(appName, equalTo("My Application"));
}
}
I can successfully run robolectric tests if I am using the new gradle
project structure but not if I am using old eclipse project structure like
the one mentioned above.

No comments:

Post a Comment