Robolectric + Gradle Test initialization failed

I cannot run tests with Robolectric when I try to run tests:

./gradlew test 

I get the following exception:

 com.Makeupalley.test.HomeRobolectricTest > initializationError FAILED java.lang.NoClassDefFoundError Caused by: java.lang.ClassNotFoundException java.lang.NoClassDefFoundError: android/app/Activity at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) . . . Caused by: java.lang.ClassNotFoundException: android.app.Activity at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 

The test fails with any statement like this: assertTrue (true).

This is my build.gradle:

 buildscript { repositories { mavenCentral() maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } } dependencies { classpath 'com.android.tools.build:gradle:0.12.+' classpath 'org.robolectric:robolectric-gradle-plugin:0.12.+' } } ... dependencies { compile('junit:junit:4.11') { exclude module: 'hamcrest-core' } compile('org.robolectric:robolectric:2.3') { exclude module: 'classworlds' exclude module: 'commons-logging' exclude module: 'httpclient' ... } } 

Test file:

 @Config(emulateSdk = 18) @RunWith(RobolectricTestRunner.class) public class HomeRobolectricTest { private TestActivity activity; public HomeRobolectricTest(){} @Before public void setup() { activity = Robolectric.buildActivity(TestActivity.class).create().get(); } @Test public void testInstanceActivity() throws Exception { assertTrue(true); } ... } 

Can someone help me give me some recommendations?

If you need more information or the build.gradle file, I can publish it.

build.gradle file: https://gist.github.com/anonymous/d0f0bac71a0fb76f8454

thanks

UPDATE - Android section

 android { compileSdkVersion 15 buildToolsVersion '19.1.0' defaultConfig { minSdkVersion 10 targetSdkVersion 15 versionCode 45 versionName "1.4.210" testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" } buildTypes { release { runProguard true proguardFile 'proguard-project.txt' } debug { runProguard false testCoverageEnabled = true } } signingConfigs { } packagingOptions { exclude 'META-INF/INDEX.LIST' exclude 'META-INF/INDEX' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE' exclude 'LICENSE.txt' exclude 'META-INF/license.txt' exclude 'META-INF/notice.txt' exclude 'META-INF/ASL2.0' } sourceSets { androidTest { setRoot('src/test') } } lintOptions { checkReleaseBuilds false abortOnError false } } 
+5
source share
2 answers

Starting with Android gradle version 1.1.0 for Android, there is support for unit testing, so you can use testCompile in gradle files. Turn it on with settings> gradle> experimental and update the gradle plugin version:

 buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.1.0' } } 
0
source

I have the following setup:

application /build.gradle

 dependencies { testCompile 'junit:junit:4.12' testCompile "org.mockito:mockito-core:1.9.5" testCompile "org.robolectric:robolectric:2.4" } 

With Gradle versions below 2.5 it also didn't work for me. Since they released the new version 2.5 a few days ago, I gave her a try and this solved the problem for me.

gradle / wrapper / gradle -wrapper.prooperties

 #Wed Apr 10 15:27:10 PDT 2013 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip 
0
source

Source: https://habr.com/ru/post/1200816/


All Articles