Questions tagged [robolectric]

Robolectric is a unit testing framework that allows Android applications to be tested on the JVM in seconds without using an emulator or device.

Robolectric allows to run Android tests inside the JVM on your workstation. Running tests on an Android emulator or device is often slow. Building, deploying, and launching the app often takes a minute or more.

Robolectric is open sourced under MIT license.

Project website: robolectric.org

1684 questions
27
votes
7 answers

Robolectric: IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

I'm new to Robolectric and instrumentation testing in general on Android. I followed along the guide for Robolectric for testing my first test. However I'm getting: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or…
timr
  • 6,668
  • 7
  • 47
  • 79
27
votes
5 answers

junit testing with gradle for an android project

I am trying to get tests ( junit and robolectric ) working in an Android project but am totally stuck. My main problem is that all testing I found with gradle somehow pull in the java plugin and then I get this error: The 'java' plugin has been…
ligi
  • 39,001
  • 44
  • 144
  • 244
27
votes
2 answers

Testing async tasks with robolectric

Do you know how to implement unit testing for AsyncTasks using Robolectric ? Any pointers will be appreciated.
bianca
  • 7,004
  • 12
  • 43
  • 58
26
votes
3 answers

Moshi 1.9.x Cannot serialize Kotlin type

I'm running into the following crash and stack trace after upgrading to Moshi 1.9.1 (from 1.8.0): java.lang.IllegalArgumentException: Cannot serialize Kotlin type com.garpr.android.data.models.RankedPlayer. Reflective serialization of Kotlin classes…
Charles Madere
  • 6,642
  • 5
  • 35
  • 34
26
votes
6 answers

JaCoCo doesn't work with Robolectric tests

I wanted to generate code coverage reports on my JUnit tests in my android project so I added the JaCoCo gradle plugin. This is my project level build.gradle file: apply plugin: 'jacoco' buildscript { repositories { jcenter() } …
Longi
  • 3,913
  • 2
  • 28
  • 38
24
votes
1 answer

Build local unit tests (No instrumentation registered! Must run under a registering instrumentation)

Take a look at the official documentation. The section Include framework dependencies gives an example of how to set up a local unit testing to work with the environment android sdk. But if you do everything as in the example, the test does not…
maXp
  • 1,428
  • 1
  • 15
  • 23
24
votes
1 answer

I need both Robolectric and Mockito in my test, each one proposes their own TestRunner

I need both Robolectric and Mockito in my test, each one proposes their own TestRunner, what do I do? I have this code: @RunWith(MockitoJUnitRunner.class) @EBean public class LoginPresenterTest { @Bean LoginPresenter loginPresenter; …
Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180
24
votes
3 answers

Jvm options in android when run gradlew test

I have a project that using Robolectric for unit test purpose. This project uses Robolectric 3.0 and need to add -ea and -noverify options in Virtual Machine options. In Android Studio, I created new JUnit configuration in Run > Edit…
23
votes
4 answers

How to resolve duplicated gradle Dependency issues

I have tried to test my code with robolectric. Problem is that it has duplicated References. e.g. java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class org.apache.maven.artifact.Artifact found in modules maven-ant-tasks-2.1.3.jar…
finder2
  • 842
  • 1
  • 11
  • 30
23
votes
4 answers

Android Robolectric Click RecyclerView Item

Is there any way to simulate a click on a RecyclerView item with Robolectric? So far, I have tried getting the View at the first visible position of the RecyclerView, but that is always null. It's getChildCount() keeps returning 0, and…
Eliezer
  • 7,209
  • 12
  • 56
  • 103
23
votes
9 answers

How to access files from assets folder during tests execution?

How to access files from assets folder during unit tests execution? My project is build using Gradle, I use Robolectric to run tests. It seems like gradle is being recognizing the assets: This is how I'm struggling to read the file: public String…
Eugene
  • 59,186
  • 91
  • 226
  • 333
23
votes
6 answers

Gradle + Robolectric: Where do I put the file org.robolectric.Config.properties?

I'm trying to setup a test using Robolectric to click on a menu button in this repository. Basic Robolectric tests will run, but I'm not able to run any project-specific test using resources because it says it can't find my AndroidManifest.xml. …
Rob Hawkins
  • 451
  • 1
  • 5
  • 17
22
votes
2 answers

How to use robolectric to test started intent with extra data

In an activity, I started a new Intent with some random extra data: Intent newIntent = new Intent(this, UserActivity.class); newIntent.putExtra("key", generateRandomKey()); startActivity(newIntent); I tested it like this: Intent intent = new…
Freewind
  • 193,756
  • 157
  • 432
  • 708
21
votes
4 answers

How do you force a configuration change in an Android Robolectric test?

I'm using robolectric to make my android unit tests fast enough to be useful. I want to test that code I've written works while the screen orientation is changing to simulate a common real world use case. Specifically what I'm testing is an…
davidjnelson
  • 1,111
  • 12
  • 22
21
votes
2 answers

@Config constants parameter could not be found in Robolectric

I am trying to write a Robolectric test. I was following few tutorials where they seem to be using @RunWith(RobolectricTestRunner::class) @Config(constants = BuildConfig::class) to setup the test, but in my case The parameter constants does not…
erluxman
  • 18,155
  • 20
  • 92
  • 126