FAQ Database Discussion Community


I am studying Robolectric but an error occured

java,android,testing,robolectric,robolectric-gradle-plugin
I am studying Robolectric. My project dir is, My unit test codes MainActivityTest are as follows, package com.example.activity; import android.app.Activity; import com.example.BuildConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.Robolectric; import org.robolectric.RobolectricGradleTestRunner; import org.robolectric.annotation.Config; import static org.junit.Assert.assertTrue; @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class) public class MainActivityTest { @Test...

Creating custom shadows with Robolectric 2.X

android,robolectric
I am running Robolectric 2.x(2.4) on my project. I have followed the example in the official doc to create a custom shadow of my own class but Robolectric is not using it. The project is on GitHub(branch roboMockitoTutorial). Here is some pieces of the code: The class which I try...

shadowOf() undefined in Robolectric-3.0-rc3.jar

android,robolectric
I am using Eclipse IDE with Robolectric unit test case framework (robolectric-3.0-rc3.jar) to write unit test case in android application test project. MainActivity activity = Robolectric.setupActivity(MainActivity.class); activity.findViewById(R.id.myvideoview).performClick(); Intent expectedIntent = new Intent(activity, CategoryActivity.class); assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(expectedIntent); ...

What should I annotate when I unit test the GPS function with Robolectric?

unit-testing,annotations,robolectric
I am referring the following site http://googletesting.blogspot.ca/2010/12/test-sizes.html . There it was mentioned that we should annotate @LargeTest if our test method is accessing the network feature. I am using Roboloectric for unit testing. And my method uses the shadowLocationManager to simulate the GPS location. I am not sure what should...

Robolectric R$color classDefNotFound

android,unit-testing,robolectric
I am trying test an android library with the following set up. com.android.tools.build:gradle:1.2.0, org.robolectric:robolectric:3.0-rc2, android studio 1.2-rc-3 In the setup for my test I am making an indirect reference to the R file of another library. The test runs fine from within android studio but running from command line produces...

Edit app.iml in Android Studio 1.2.1.1

android,intellij-idea,android-studio,junit,robolectric
After 3 days struggling to make JUnit and Robolectric to work with Android Studio I finall did it, but there is one important step I have to make everytime I open the IDE. I have to go to app.iml and change "false" to "true" in "ALLOW_USER_CONFIGURATION". After this the testing...

Code Coverage for Android Library Project while using Robolectric

android,ant,code-coverage,robolectric,eclemma
I have an android library project. We are using Robolectric to run the unit tests. Unit Tests are working fine from the IDE as well as Ant build.xml file. I am able to get the code coverage using the eclEmma plugin in Eclipse. But I am facing problems while getting...

Robolectric in CI environment

android,robolectric,android-testing,circleci
I'm running into an issue where my robolectric tests pass in my terminal with: ./gradlew test and the same command, properly configured with all the tools on circleci gives me this error: java.lang.RuntimeException: java.lang.IllegalArgumentException: maxSize <= 0 at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:238) at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:185) at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54) at...

GridView.getChildAt returns null when testing with Robolectric

android,gridview,android-listview,robolectric
I am trying to test a GridView to ensure that it is loading images with Robolectric. GridView.getChildAt(0) returns null even though there is data in the adapter. I understand that this is because getChildAt() returns based on what is shown on screen. Is there a way to get this test...

Robolectric 3.0 java.lang.NoSuchMethodException on create activity

java,android,testing,robolectric
I was excited to hear that android studio had made some updates to make unit testing simple to do in android studio so I have been trying to set up roboelectric but I cant get past this error that occurs during the following method. MainActivity = Robolectric.buildActivity(MainActivity.class).create().get(); with the following...

NoClassDefFoundError: … R$styleable when using Robolectric with Android Library (AAR)

java,android,robolectric
We are developing a library that provides an Activity, and I would like to be able to perform some functional testing on the Activity. My build.gradle includes: dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' testCompile 'org.mockito:mockito-core:1.+' testCompile 'junit:junit:4.12' testCompile('org.robolectric:robolectric:3.0-rc2') { exclude group: 'commons-logging', module: 'commons-logging' exclude group: 'org.apache.httpcomponents',...

How come PopupWindow.OnDismissListener is not called when using Robolectric?

android,robolectric,android-popupwindow
I have a problem. I want to write a test for code that relies on execution of the PopupWindow.OnDismissListener.onDismiss() method. However, it never seems to be called. Am I doing something wrong? Sample code: View content = new View(Robolectric.application); PopupWindow popup = new PopupWindow(content, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); popup.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override...

InjectView doesn't work in Unit Testing

android,android-studio,gradle,robolectric,roboguice
tl;dr: In Unit Testing, @Inject successfully injects the fields required but @InjectView leaves them as null, causing errors. @InjectView works in normal application running. I have an Android application that utilises RoboGuice that I'm trying to Unit Test. One of the activities I'm trying to test looks like this: @ContentView(R.layout.activity_login)...

Make Robolectric run test for the current build type only

android-studio,robolectric
I'm using Robolectric alongside Android Studio (v 1.2.2) classpath 'com.android.tools.build:gradle:1.2.3' buildToolsVersion "21.1.2" testCompile 'org.robolectric:robolectric:3.0-rc3' My project contains 4 build types. In spite of having debug build type selected in the "Build Variants" window, Robolectric tests every declared build type (4 times). Is it an expected behavior? How can I narrow...

Robolectric test ListView not updating after value changes in adapter

android,listview,android-listview,robolectric
I have created a layout with a ListView that updates values in it depending on which of the 2 given buttons are pressed. Pressing the button labelled Odds clears all values and adds odd numbers up until 20. Pressing evens does the same except for even numbers. This works fine...

Using Robolectric, how to init and finalize stuff before and after all test methods in a test class is ran

java,android,unit-testing,robolectric
I'm using Robolectric to do unit testing. I have a test class say MainActivityTest, which has several test methods: @RunWith(...) @Config(...) public class MainActivityTest { @Test public void testMethod1() { //... } @Test public void testMethod2() { //... } // other test methods } I want to execute a method(init...

Android Studio unit testing support vs robolectric

java,android,unit-testing,android-studio,robolectric
Hitherto I used robolectric unit testing with JUnit 4.x for testing my business logic. In the last version of Android Studio 1.1.0 was announced native support of unit testing junit:4.+. Should I refuse using robolectric? Has robolectric some distinct advantages that I might not know? As for me using Android...

Robolectric 3 : Load test specific resource

android,unit-testing,robolectric
I'm using Robolectric 3 and I'm trying to load some resources that are in my test/res/ folder. Here is my file hierarchy : project/ |-src/ |--main/ |---AndroidManifest.xml |---res/ |----raw/ |-----prod.json |--test/ |---java/ |----com.app/ |-----CustomTestRunner.java |-----Mytest.java |---res/ |----raw/ |-----test.json I would like in MyTest.java to do something like RuntimeEnvironment.application.getResources().openRawResource(R.raw.test); but i...

Difference between preference fragment and “regular” fragment

android,android-layout,android-fragments,robolectric
I am attempting to write some tests to "assert" the headers of my settings app are being properly built. The settings app was built by someone else so I am attempting to understand their code before doing anything. They used "Preference Fragments" in this app. what is the difference between...

Run Android test with Robolectric - dependency error

android,robolectric
I am using Android Studio 1.2 and Windows 7 When running a robolectric test following this example: @RunWith(CustomRobolectricRunner.class) @Config(emulateSdk = 21, reportSdk = 21, constants = BuildConfig.class) public class PasswordProviderTest{ @Test public void testGetPassword() throws Exception { Activity activity = Robolectric.setupActivity(LoginActivity.class); } @Test public void testSetPassword() throws Exception { }...

Manifest and setup issues getting Robolectric working with Android Studio 1.1.0

android,testing,robolectric,robolectric-gradle-plugin
I am trying to get Robolectric tests up and working in our current project, and not having a lot of luck. My preference would be to get these to run within Android Studio 1.1.0+. This is my project structure: and here is my test: import android.widget.Button; import com.mycompany.android.app.R; import org.junit.Before;...

Android build tools 1.1.0, unit test folder?

android,android-gradle,robolectric,android-build,build-tools
I recently installed the latest tools from google to my android project: buildscript { repositories { jcenter() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:1.1.0' } } allprojects { repositories { jcenter() } } apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion "21.1.2" compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } defaultConfig {...

Run robolectric tests in order?

android,testing,junit,robolectric
I've got Robolectric test suite for my app. As I understand, by-default Robolectric runs tests in alphabetical order. I have tests testA() testB() This tests need to run in reverse order, e.g. testB() changes tested activity state to whatever testA() needs. How do I specify order in which tests must...

AndroidStudio Robolectric Unit Tests - Invaild default

java,android,unit-testing,junit,robolectric
I want to use Unit Testsfor my Android Application. First, I've tried the Unit-Tests with Robolectric with a template Project "deckard-gradle-master". As it worked fine, I wanted to integrate this testing in my existing Android Application. I have added the libraries hamcrest-core-1.3.jar, junit-4.12.jar, robolectric-2.4.jar and robolectric-gradle-plugin-1.0.1.jar. When i try to...

Mock native method with a Robolectric Custom shadow class

class,mocking,shadow,robolectric
I have a class with a regular method and a native method that I'd like to mock: public class MyClass { public int regularMethod() { ... } public void native myNativeMethod(); } I am using Robolectric to test my app, and I am trying to figure out a way to...

Android testCompile project not working

android,gradle,robolectric
I am using Robolectric and the "android-unit-test" plugin in my android project. I have multiple modules (eg foo and bar) and would like to use the same test runner across all the modules. To achieve this I created a Test module to hold my test runner and any custom shadow...

Why is android studio instantiating tests each time?

android,unit-testing,android-studio,robolectric
When I try to run unit tests in android studio, I can see it make, build gradle, etc... all the build stuff, and then I can see an instantiating tests ... which counts up to 9 every time, even if I don't change anything. In total, running a single test,...

How to use Robolectric 2.4 in Android Studio 1.1.0 & gradle 2.2.1

android,android-studio,gradle,robolectric
I am unable to configure my Android Studio Project with Robolectric. Could any one guide me step by step to build an android studio 1.1.0 project with Robolectric 2.4 & gradle 2.2.1 This sample projects work fine https://github.com/robolectric/deckard-gradle. But I am not able to implement it in my existing Android...

How to test ListView items from server using Robolectric 3.0

android,unit-testing,testing,robolectric,robolectric-gradle-plugin
I am using Robolectric to test my Application. In my app I want to check the listview is there any items in that view. And test the onClickItem from the listview. The listview items is dynamic from server. Every time the activity is started it will request from server. But,...

Confused about testCompile and androidTestCompile in android gradle

android,gradle,robolectric
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 gradle build script when referencing robolectric while...

Android Gradle 1.1 Module Unit Test dependency on another Android Module in same Project

android,android-studio,gradle,android-gradle,robolectric
I have an Android Gradle 1.1 Project with several Modules that have some dependency on the others. Project ├ Module2 │ ├ src/main: imports classes from Module1 │ └ src/test: imports classes from Module1, tests Module2/src/main └ Module1 ├ src/main: implements classes needed by Module2/src/main & Module2/src/test └ src/test: tests...

Resources$NotFoundException when calling Robolectric.buildActivity()

java,android,unit-testing,junit4,robolectric
I am using Robolectric 3.0 RC3 for unit testing in Android Studio. And I am getting a ResourceNotFoundException every time I run the test. Please help me resolve the issue. build.gradle dependencies { testCompile 'junit:junit:4.12' compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.0.0' testCompile 'org.mockito:mockito-core:1.9.5' testCompile 'org.robolectric:robolectric:3.0-rc3' compile 'com.ibm.icu:icu4j:53.1' } Test...

NoClassDefFoundError when trying to test Android app with Gradle, Robolectric and Travis CI

android,gradle,robolectric,travis-ci
I am always running into NoClassDefFoundError when using Travis CI to test an Android application with Robolectric and Gradle. All tests pass in Android Studio. So I guess it's a Travis specific configuration issue. Any experience in running Robolectric tests with Gradle and Travis? I've the following .travis.yml: language: android...

Robolectric accessing database throws an error

android,android-sqlite,robolectric
I have a test that creates an activity which attempts to get some data from the database. This fails with SQLiteException 17:40:40.528 [DEBUG] [TestEventLogger] android.database.sqlite.SQLiteException: Cannot open SQLite connection, base error code: 14 17:40:40.528 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowSQLiteConnection.rethrow(ShadowSQLiteConnection.java:53) 17:40:40.528 [DEBUG] [TestEventLogger] at org.robolectric.shadows.ShadowSQLiteConnection.access$600(ShadowSQLiteConnection.java:30) 17:40:40.529 [DEBUG]...

“Activity has been destroyed” error on Fragment testing with Robolectric

android,android-fragments,robolectric
I'm trying to make Fragment testing on Android with Robolectric and I know there's been some other posts about problems with FragmentActivity when it hasn't been stored in some variable. However, I was trying to make this post work and you can see in this case the FragmentActivity object reference...

Robolectric Samples and Gradle Plugin

android,android-studio,robotium,robolectric
In Robolectric sample project, there is no gradle plugin set up in build.gradle. For instance for AppCompat-v7 On the other hand we have Robolectric gradle plugin which as documentation says: A Gradle plugin which enables Robolectric tests. This plugin piggy-backs on the unit testing support added in version 1.1.0 of...

How to unit test the GPS location in android using Robolectric?

java,android,unit-testing,robolectric
I have an android app that gives the current location when button is tapped. I want to unit test this functionality . I'm using Robolectric . The issue is ,the system services are accessed in onCreate() method but not out of it. If that is the case,how can I access...

Roboelectric issues with new gradle and android sdk versions

android,junit,robolectric
I used to be able to use roboelectric with gradle just fine until recently. I keep getting an error Error:(6, 17) error: package org.junit does not exist. I'm not quite sure and have dug into this quite a bit. Below is my project build.gradle: // Top-level build file where you...