Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Robolectric Exception

I am facing a problem with Robolectric library. I am writing unit tests using Robolectric, it's working locally but when I merge my code, it crashes on pipeline(remotely). I am using 'org.robolectric:robolectric:4.0.2' It fails by only adding this line to my test class: @RunWith(RobolectricTestRunner.class)

And the exception is:

FAILED org.apache.tools.ant.BuildException Caused by: org.apache.maven.artifact.resolver.MultipleArtifactsNotFoundException

like image 220
Najoua Mahi Avatar asked May 02 '19 11:05

Najoua Mahi


People also ask

What is robolectric in Android?

Robolectric handles inflation of views, resource loading, and lots of other stuff that’s implemented in native C code on Android devices. This enables you to run your Android tests in your continuous integration environment without any additional setup. Robolectric supports resource handling, e.g., inflation of views.

What are the limitations of robolectric?

Robolectric is not an integration test framework, i.e., you cannot not test the interaction of Android components with it. Robolectric does not require additional mocking frameworks, of course it is still possible to use frameworks like Mockito if desired. 1.2. Shadow objects

How to test robolectric framework?

Robolectric is not an integration test framework, i.e., you cannot not test the interaction of Android components with it. Robolectric does not require additional mocking frameworks, of course it is still possible to use frameworks like Mockito if desired. 1.2. Shadow objects Robolectric replaced all Android classes by so-called shadow objects .

What are the main features of robolectric?

These are the main features of it: It provides a way to run our tests inside Android Studio, without launching app on Device or Emulator. Shadow Classes, rewritten android core libraries by Robolectric, are the real magic of Robolectric. It is a replica of Android class with some exposed useful functions.


1 Answers

I had the same issue: Robolectric was working fine locally, but once pushed to Jenkins the gradle task to execute the tests fails.

You can execute the gradle task to run the tests with -i -d flags to see more debug output.

./gradlew -i -d test

For me this revealed that Jenkins was unable to download Robolectric dependencies:

13:58:43  13:58:42.904 [DEBUG] [TestEventLogger] com.my.package.Test > my_test_case STANDARD_ERROR
13:58:43  13:58:42.904 [DEBUG] [TestEventLogger]     Downloading: org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.jar from repository sonatype at https://oss.sonatype.org/content/groups/public/

I could solve it by telling gradle to use our corporate proxy when running on Jenkins. One way to achieve this would be to add the following to your gradle.properties:

systemProp.http.proxyHost=http://proxy.host
systemProp.http.proxyPort=3128
systemProp.https.proxyHost=http://proxy.host
systemProp.https.proxyPort=3128

---- EDIT ----

Actually I found a cleaner solution for my use case then configuring a proxy: Robolectric offers a way to configure the repository it uses during runtime (see http://robolectric.org/configuring/). This way I was able to tell it to use our corporate repository.

android {
  testOptions {
    unitTests.all {
      systemProperty 'robolectric.dependency.repo.url', 'https://local-mirror/repo'
      systemProperty 'robolectric.dependency.repo.id', 'local'
    }
  }
}
like image 52
Christian.D Avatar answered Sep 25 '22 22:09

Christian.D