android - Robolectric with Gradle: package org.robolectric does not exist -


i trying run robolectric tests gradle android build system. have followed instructions given here try make work while running tests using gradle robolectric badly stuck @ following error -

/users/sreekanth/documents/artoo/code/android/helloworld/src/test/java/com/example/helloworld/mytest.java:8: package org.robolectric not exist import org.robolectric.robolectrictestrunner; 

my project has 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 build.gradle:

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 run robolectric tests if using new gradle project structure not if using old eclipse project structure 1 mentioned above.

you need use new structure, period. robolectric plugin has path hardcoded. set sourceset build script hacky , not "official".


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -