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
133
votes
14 answers

How can we access context of an application in Robolectric?

Actually, I need to get a response of an API call, for that I required Context.
user1667968
  • 1,489
  • 3
  • 12
  • 16
91
votes
1 answer

Testing ViewPager (and CursorLoader) with Robolectric

Does anyone know how to test the following setup using Robolectric? Fragment containing a ViewPager, data loaded with a CursorLoader. With the code below, the CursorLoader is never pushed into the adapter for the view pager. I get stuck at the…
Corey Scott
  • 2,430
  • 3
  • 28
  • 33
87
votes
4 answers

Confused about testCompile and androidTestCompile in Android Gradle

I'm new to testing world and even more to Android testing world. While doing research on Robolectric that aids with tests on android one thing confuses me the most. Sometimes on the web I see people using testCompile keyword in dependencies of the…
Lucas
  • 3,521
  • 3
  • 27
  • 39
85
votes
6 answers

Does Robolectric require Java 9?

All the tests are passing, but I get the below warning. Robolectric is telling me that Java 9 is required. I am using the latest version of Robolectric. [Robolectric] WARN: Android SDK 10000 requires Java 9 (have Java 8). Tests won't be run on SDK…
The_Martian
  • 3,684
  • 5
  • 33
  • 61
84
votes
4 answers

Testing custom Views with Robolectric

I'm trying to run unit tests with Robolectric 2.1.1 and I cannot get it to inflate custom layouts (e.g. ViewPagerIndicator classes). Suppose this is my layout:
Tadej
  • 2,901
  • 1
  • 17
  • 23
83
votes
1 answer

Can I use Cobertura on Unit Tests with PowerMock?

Problem I am setting-up unit-test code coverage for an Android library which uses Robolectric to run the tests and PowerMock/Mockito for mock-testing. However, running unit-tests with Cobertura results in the following…
PLNech
  • 3,087
  • 1
  • 23
  • 52
73
votes
3 answers

Does Robolectric support API level?

I have some Test which I would like to run with Robolectric, I use the 2.3-SNAPSHOT as my APP uses the ActionbarCompat i needed to use 2.3-SNAPSHOT Version as Robolectric could not find the AppCompat Themes before. So I setup the Classpath in…
Kitesurfer
  • 3,438
  • 2
  • 29
  • 47
63
votes
9 answers

kotlin and ArgumentCaptor - IllegalStateException

I have a problem with capturing the Class argument via ArgumentCaptor. My test class looks like this: @RunWith(RobolectricGradleTestRunner::class) @Config(sdk = intArrayOf(21), constants = BuildConfig::class) class MyViewModelTest { @Mock …
Ramps
  • 5,190
  • 1
  • 45
  • 47
61
votes
4 answers

Robolectric vs Android Test Framework

Does Robolectric provide any clear benefits compared to Android Test Framework ? I've read the docs regarding both the frameworks but as far as i can see the only clear cut benefit regarding Robolectric is that it runs on JVM rather than the…
Traxex1909
  • 2,650
  • 4
  • 20
  • 25
59
votes
7 answers

How can I test fragments with Robolectric?

I know there is a Robolectric.shadowOf(Fragment) method and a ShadowFragment class, thought they aren't listed on the docs, but I can't make it work. myFragment = new MyFragment(); myFragment.onCreateView(LayoutInflater.from(activity), (ViewGroup)…
kaneda
  • 5,981
  • 8
  • 48
  • 73
53
votes
3 answers

Android Unit Test with Retrofit2 and Mockito or Robolectric

Can I test real response from retrofit2beta4? Do i need Mockito or Robolectic? I don't have activities in my project, it will be a library and I need to test is server responding correctly. Now I have such code and stuck... @Mock ApiManager…
AndrewS
  • 7,418
  • 8
  • 35
  • 50
52
votes
6 answers

Where is log output written to when using Robolectric + Roboguice?

I'm using Robolectric to test Android. I'm running my tests via maven, e.g. mvn -Dtest=LogTest test If I have code that writes to the logs, such as Log.d("TAG", "blah"); or using Roboguice's Ln Ln.d("blah"); I don't see any output in maven's…
Tyler Collier
  • 11,489
  • 9
  • 73
  • 80
51
votes
4 answers

Providing test data for SharedPreferences for Robolectric

Just started to use Robolectric and it seems to be pretty much what I need. However, I've hit a bit of a roadblock with regards to the use of SharedPreferences. I have two tests cases Activity expects a new/empty sharedPreferences Activity…
pyko
  • 3,399
  • 5
  • 26
  • 31
50
votes
8 answers

Robolectric: Resources$NotFoundException: String resource ID with Android Gradle Plugin 3

Android Studio 3.0 Beta2 classpath 'com.android.tools.build:gradle:3.0.0-beta3' testCompile 'org.robolectric:robolectric:3.4.2' Test class that I am using that fails to run: @Config(constants = BuildConfig.class, sdk =…
ant2009
  • 27,094
  • 154
  • 411
  • 609
49
votes
2 answers

How to test remote android aidl service

I have a small app that interacts with a remote android service. I would like to mock that service in unit tests. I use Robolectric and JUnit for other test cases and shadows but I could not figure how to deal with remote services. Is it sufficient…
Anders T.
  • 499
  • 4
  • 4
1
2 3
99 100