android,gridview,android-listview,robolectric
This is not as thoroughly testing the system but might be the best that can be done. @Test public void shouldLoadImageToGrid() throws Exception { GridView gridView = (GridView) mImageGridFragment.getView().findViewById(R.id.gv_image_grid); ImageGridData data = (ImageGridData) gridView.getAdapter().getItem(0); assertEquals((long)R.drawable.sample_image, (long)data.getImageId()); } ...
unit-testing,debugging,logging,robolectric,gradlew
I often doing recently (because of lack integration with Android Studio) next in my test code: System.out.println("TEST"); And run gradle with: gradle testDebug -i ...
android,android-studio,gradle,android-gradle,robolectric
The secret is to have project.properties and test-project.properties file next to your AndroidManifest.xml At the test-project.properties insert all your exploded aar resource path (e.g. android.library.reference.3=../../build/intermediates/exploded-aar/AndroidStudioAndRobolectric/core/unspecified) I explain this a bit more at http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html And a ready to use example can be found at https://github.com/nenick/AndroidStudioAndRobolectric/tree/library...
android,unit-testing,gradle,android-gradle,robolectric
I was searching and didn't find answer that I thought already covered this. So decided to create new one for the future. Answer Android Studio is not picking up unit tests automatically right now. I know it is planned for 1.3 version. So you have to change test artifact value...
android,gradle,out-of-memory,robolectric
The information that I have gathered. Abdellah gave some insight on the problem and it has to do with the number of total classes created. "you have check https://plumbr.eu/outofmemoryerror/permgen-space " Eugen Martynov also mentioned the parameter from the robolectric gradle plugin configuration that I can add. Link at https://github.com/robolectric/robolectric-gradle-plugin#configuration-using-dsl The...
java,android,unit-testing,android-studio,robolectric
There is no reason you cannot use both. Robolectric has the advantage that you can test against actual Android behaviour when needed. Plain JUnit will just let you test the parts of your code that do not interact with Android. This will let you view the results of the robolectric...
It is quite simple. Let assume you have next: productFlavours { one two } So to run tests for on flavour you simply run gradle: gradle testOneDebug To run all tests for all variants: gradle test ...
android,sqlite,unit-testing,robolectric
You will need to put the .db file under src/test/resources/ folder. For example, sample.db Then in your unit test setUp() call: @Before public void setUp() throws Exception { String filePath = getClass().getResource("/sample.db").toURI().getPath(); SQLiteDatabase db = SQLiteDatabase.openDatabase( (new File(filePath)).getAbsolutePath(), null, SQLiteDatabase.OPEN_READWRITE); // perform any db operations you want here } ...
You have to set the build variant test artifact to Unit Tests. I no longer needed the "core" project, so I deleted it. My build.gradle for my app looks like this: repositories { jcenter() } apply plugin: 'com.android.application' android { ... sourceSets { androidTest.setRoot('src/test') } } dependencies { ... //...
android,tdd,junit4,robolectric
Ok, I managed to setup it! It is very tricky and not fully compatible with the newest Gradle 1.0.0, but it is working like a charm! My solution is based mainly on this tutorial So, your build.gradle file (the "inner" one, inside the project's folder) should look like this: buildscript...
android,robolectric,android-testing,circleci
The solution was to add a memory cache to ImageLoader as below: ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this) .memoryCache(new WeakMemoryCache()) .build(); ...
android,testing,robolectric,sugarorm
Ok try next. Add next class to you test code: public class TestSugarApp extends SugarApp { @Override public void onCreate() {} @Override public void onTerminate() {} } The class named Test will be loaded and used by Robolectric and you can override some things that are not relevant for testing....
android,unit-testing,mockito,robolectric,retrofit
I'm still wrapping my head around how to properly use rxjava myself but I would try to modify your code so that you only observeOn(mainThread) on the final zipped Observable instead of doing it on both of the original request response's Observable. I would then verify if this affect the...
Simply testCompile is the configuration for unit tests (those located in src/test) and androidTestCompile is used for the test api (that located in src/androidTest). Since you are intending to write unit tests, you should use testCompile. Update: The main distinction between the two is the test sourceset runs in a...
android,android-studio,robolectric
You need to install the Android Studio Unit test plugin for Android Studio to pick up the src/test/java folder.
Should be something like: @Test public void sent_message_list_item_should_autolink() { TextView textView = (TextView) activity.findViewById(R.id.myView); assertEquals(Linkify.ALL, textView.getAutolinkMask()); } Also consider of using some assertions library. My preference is Android AssertJ...
java,android,unit-testing,robolectric
You want @BeforeClass and @AfterClass. Note that the methods they annotate have to be static. See: http://junit.sourceforge.net/javadoc/org/junit/BeforeClass.html http://junit.sourceforge.net/javadoc/org/junit/AfterClass.html So for example: @BeforeClass public static void beforeEverything() { // runs just once before all tests } @Before public void setup() { // runs before every test } @After public void breakdown()...
android,android-studio,gradle,robolectric,roboguice
As it turned out this had nothing to do with Roboguice per se. There were some misconfigurations that were particular to the OP's setup. Solved it over chat. The issues were using Roboguice's @ContentView (it does not play nice with tests apparently, the same happened to me in a previous...
android,testing,junit,robolectric
It's not recommend to use fixed test order but you could try to use @FixMethodOrder(MethodSorters.NAME_ASCENDING) at your test classes. See also the discussion here How to run test methods in specific order in JUnit4?...
android,android-layout,android-fragments,robolectric
Preference fragments are very different from regular fragment. Their layout is defined by the system and the content is inflated from XML resources. The purpose of this is to provide consistent settings screens in your apps. Have a look at the API Guide for more info.
android,android-fragments,robolectric
When you call activity.recreate() you actually destroy your current activity and the reference you keep becomes invalid. You then try to use the FragmentManager of a destroyed activity and it crashes.
android,robolectric,android-popupwindow
The reason why the listener isn't called is because the ShadowPopupWindow from Robolectric overrides the default implementation and simply doesn't call it. Code from source: public void dismiss() { if (context != null) { getWindowManager().removeView(containerView); } showing = false; } Depending on what it is you need to test, there...
You should wrap Parse API to some class and mock it: public class ParseAPI { public void init(Context context, String key, String id) { // Add your initialization code here Parse.initialize(context, key, id); } public void initPush(Context context, Class pushCallbackClass) { // Specify an Activity to handle all pushes by...
So the root of the problem seems to be how gradle does (or does not) handle creating android/support/v7/appcompat/R.java for com.android.library modules. With clues from here I was able to get things working both from the command line and Android by putting this chunk in my build.gradle def package_namespace = "com.moxiesoft.netagent.androidchatcustomerclient"...
Dear guys,I has figure it out! @Config(manifest = "../../sampleProject/app/AndroidManifest.xml")...
java,android,robolectric,android-testing
I can only post my opinion about that topic. My preferred way is the MVP pattern. java classes where you mock away views (android stuff) and test pure logic android classes where i mock my presenter and similar classes, the initialisation will be done by robolectric with just create phase....
java,android,testing,robolectric
If you writing unit tests, then you definitely can use spy's and mock's from Mockito library. Add Mockito library dependency: testCompile 'org.mockito:mockito-core:2.0.3-beta' Create spy Activity in your test method: Activity activity = spy(Activity.class); You can read more about spy and mock objects for better understanding of their purposes....
android,unit-testing,mockito,robolectric
I ended up creating my own solution to this. My approach was to add another level of indirection to each my calls that create or set an object. First, let me point out that I couldn't actually get Mockito to work reliably with Fragment or Activity objects. It was somewhat...
java,android,facebook,mockito,robolectric
There are many things wrong with your test. You're creating a spy, but not actually using it within your test. You're trying to verify that a method has been called, before calling the code that will cause that method to be called. You're never calling onCreate, which appears to be...
android,testing,gradle,robolectric,robolectric-gradle-plugin
Made the same mistake myself. Thats the format for jcandksolutions plug-in (at least that's what I was using). -Dtest.single=<test name> is not supported by RoboE-Gradle plugin. Use --tests <test class name> mechanism. This can be either --tests <classpath of test> or wildcard with --tests *.*Test...
android,listview,android-listview,robolectric
In robolectric you must call ShadowListView.populateItems() to get adapter changes ShadowListView shadowListView = Robolectric.shadowOf(listView); shadowListView.populateItems(); ...
android,intellij-idea,android-studio,junit,robolectric
This used to be an issue, but should no longer be a problem with the new Android Studio support for unit tests. Checkout the instruction and setup in this sample project: https://github.com/robolectric/deckard-gradle...
android,mockito,robolectric,stubs
Think of a simple unit test for MyActivity class where you will not use Robolectric.setupActivity. You write a test case where you call MyActivity.onCreate to check that some preinitialisation is done when called. This test case will fail at super.onCreate call which is forced by android system. Mock will not...
android,unit-testing,robolectric
EDIT (New Response): The issue was traced to an issue when setting a color as the android:icon in the ActionBar. See the thread over on github - https://github.com/robolectric/robolectric/issues/1053 for my workaround. Old Response: I was able to fix the issue by adding <item name="android:windowNoTitle">true</item> to my theme file. However this...
Yes, this is setup time. Unfortunately there are no central place where you can gather all information about Robolectric. Watch this presentation and you will find more explanations on pages 28, 29, 30, 35. If you're interested the Robolectric changed some approaches in version 2.0. Significant changes are: Javaassist usage...
android,unit-testing,robolectric,objectanimator
I ended up creating an AnimationUtility interface and a real and fake implementations. The fake implementation immediately set the view to visible/hidden instead of doing the animation. I dynamically inject the real/fake one depending on the proper context.
android,gradle,android-studio,robolectric
You don't need write your own TestRunner. Just use the correct naming schema for your test application class: TestNSApplication (instead of NSApplicationTest) Often I see following pattern: *Test marks test execution classes Test* marks class extensions for make a class testable I expect you already know that this class must...
android,ant,code-coverage,robolectric,eclemma
This was a problem with configuration only. I changed the configuration and updated my build.xml . Now it works like a charm. I am able to get code coverage easily through Jacoco
@Test public void test() throws Exception { ServiceController<FooService> serviceController = Robolectric.buildService(FooService.class); // ... intent declaration. serviceController.attach() // Calls service.attach() .create() // Calls service.onCreate() .withIntent(intent) .startCommand(0, 1); // Calls service.onStartCommand(intent, 0, 1) } The key point is to call attach() before onCreate(). Anyone doesn't know how to create a Service instance...
android,android-studio,robotium,robolectric
Main reason is that this plugin isn't necessary and will be marked as deprecated if robolectric 3.0 get final. This is a statement from robolectric maintainer erd. See first answer at https://github.com/robolectric/robolectric-gradle-plugin/issues/148
java,android,junit,gradle,robolectric
You can use BuildConfig.VERSION_NAME constant instead of gathering it from PackageInfo. However because Robolectric tests are run only for debug configuration it is not possible to check in Robolectric tests release configuration cases. But maybe it is not your case...
android,unit-testing,android-studio,robolectric
Here is what worked for me in the end. If you go to Run/Debug Configurations and delete all the previous configurations, the tests start a lot faster. Also, doing this helps when you change between Unit tests and Instrumentation tests and you get the "ResultPrinter" error....
java,android,unit-testing,junit4,robolectric
Is your test class annotated correctly? And is it using the correct path to a manifest file? The following works for me: @RunWith(RobolectricTestRunner.class) @Config(manifest = "src/main/AndroidManifest.xml", emulateSdk = Build.VERSION_CODES.LOLLIPOP) public class MyActivityTest { .... } Removing the Config annotation causes the ResourcesNotFound exception you mentioned....
android,tdd,android-gradle,robolectric
I ran into the same issue, dug into the code, fixed it, and submitted a pull request which has just now been merged. See my explanation on the PR for details, but it boils down to a bad optimization in the plugin code: https://github.com/robolectric/robolectric-gradle-plugin/pull/70 As of today you need to...
android,unit-testing,testing,robolectric,robolectric-gradle-plugin
I thought you were missplacing the initialization. First you have to set getFakeHttpLayer().interceptHttpRequests(false). Then it should be like this : @Before public void setUp() { FakeHttp.getFakeHttpLayer().interceptHttpRequests(false); FakeHttp.setDefaultHttpResponse(200, "OK"); FakeHttpLayer fakeHttpLayer = FakeHttp.getFakeHttpLayer(); activity = Robolectric.setupActivity(HomeActivity.class); assertFalse(fakeHttpLayer.hasPendingResponses()); assertFalse(fakeHttpLayer.hasRequestInfos());...
android,android-gradle,robolectric,android-build,build-tools
I found the solution which is to switch between Test Artifacts in the left corner of the IDE. On this screen only "Android Instrumentation Tests" is available because I downgraded my android tools but with tools 1.1.0+ you should see different types of test to get the IDE recognize them...
android,gradle,robolectric,travis-ci
The issue was caused by AppCompat v7. dependency which seems to be a known issue with Travis CI. For above's use-case, removing the dependency solved the problem....
I think the following method is what you are looking for: http://robolectric.org/javadoc/org/robolectric/shadows/ShadowService.html#isStoppedBySelf()
With your given information I guess you missed one little aspect: test classes appear not in jars. When your shared TestRunner is under src/test/java then other modules will not see it. When I'm correct then place you TestRunner at src/main/java and all modules should have access to our support classes....
java,android,eclipse,parse.com,robolectric
I had the very same problem, so I created a TestApplication commenting the ParsePushBroadcastReceiver, and it worked. Notice that you must put Test before your application name (Create a new class and copy-paste the original application). Like this: public class TestMyMoneyApplication extends Application { @Override public void onCreate() { super.onCreate();...
android,robolectric,recyclerview
Seems like the issue was that RecyclerView needs to be measured and layed out manually in Robolectric. Calling this solves the problem: recyclerView.measure(0, 0); recyclerView.layout(0, 0, 100, 10000); ...
android,android-gradle,robolectric
I'm running them in Android Studio and from there you can execute one test, one test case, all tests in the package: How to run unit tests with Android Studio About command line you can take a look on this plugin: https://github.com/JCAndKSolutions/android-unit-test...
android,unit-testing,robolectric
The R class is missing until you will provide one ;) Try if it works for you too when you copy a valid R class to the expected package. You must change the paths at the copy task to match your package names afterEvaluate { project -> android.libraryVariants.each { variant...
gradle,robolectric,robolectric-gradle-plugin
Ok, this is the easiest way to do it, You will have to extend RobolectricTestRunner getAppManifest and createAppResourceLoader. In getAppManifest you will simply have to store the manifest in a field, let's say mDefaultManifest. In createAppResourceLoader you will have to add the right resources injected. /** * TODO: Watch OUT...
java,android,testing,robolectric,robolectric-gradle-plugin
Looks like you are a Mac user. Did you try your project on command line? There is a nice page with great information how to get started with robolectric: http://robolectric.org/getting-started/ and there you get the following hint. Note for Mac Users If you are on a Mac, you will probably...
android,android-studio,robolectric,robolectric-gradle-plugin
After some more digging it appears that with Robolectric 2.4 some issues with ActionBarActivity appear. According to this issue full support for appcompat-v7 will be provided in Robolectric 3.0. Indeed trying Robolectric 3.0-SNAPSHOT solved the issue.
java,android,unit-testing,junit,robolectric
Changing the @Config to @Config(manifest = "app/src/main/AndroidManifest.xml", emulateSdk = 18, reportSdk = 18) solved the problem for me.
android,eclipse,git,unit-testing,robolectric
Thanks for the advice. I ended up doing this: Move the MyProjectTest source files into folder "MyProject/tests", and - from the MyProjectTest project - created a link (in Eclipse) to that destination. I then added the new source files to the MyProject git repo. Seems to be working fine.
android,robolectric,robolectric-gradle-plugin
For various reasons, I've abandoned the above approach in favor of keeping my Robolectric tests in a Gradle submodule. You can find a blog post I wrote about why I moved in this direction and a fork of the deckard-gradle project that shows you how here. Since Robolectric only support...
You need to use Shadows.shadowOf(), it changed in Robolectric 3.0. For further reference about other changes, see this guide
android,debugging,gradle,android-studio,robolectric
It depends on the version of gradle you are using. If you are using version 1.x, then add the following script parameter to your run configuration -Dtest.single=<testfilename> e.g. -Dtest.single=MyTest You don't have to worry about the path to the file - just the name of the file containing the test...
java,android,unit-testing,junit,robolectric
We use it for: unit testing: all components from parsers and utils, to controllers and presenters integration/acceptance testing: the business logic of the app, per screen (which falls into integration and/or acceptance testing) We don't use it for (and have found it difficult to use for these): testing the network...
java,android,unit-testing,robolectric
Robolectric creates the application differently. Try this: mainActivity = Robolectric.buildActivity(MainActivity.class).create() .start().resume().get(); The NPE should go away, and the rest of the code from that tutorial should work just fine. Note that Alan Evans actually put almost the same snippet in the comments section of the post you cited....
android,unit-testing,robolectric
Instead of putting the json in src/test/res/raw you might want to put it in src/test/resources/ and then you can use it ( with the latest build plugin and latest AS ) via getResource Be aware that there is a bug in older versions - you need to use AS from...
unit-testing,android-fragments,gradle,teamcity,robolectric
So following @EugenMartynov's suggestion I added an additional assemble build step to my Teamcity build before building running the tests and so far all the builds pass. It seems that Robolectric indeed has some issues with CI builds. So to summarize, instead of building once and running tests, my build...
android,unit-testing,robolectric
Robolectric allows you to call into the Settings class and set values: Settings.Secure.putString(contentResolver, Settings.Secure.LOCATION_PROVIDERS_ALLOWED, "locationProvider"); Put what you want in there and it should return what you've set in the tests....
unit-testing,annotations,robolectric
You're right, those annotations are not for Robolectric. They are useful when using the Android InstrumentationTestRunner which runs tests on device. Robolectric runs tests on the JVM, different beast altogether. ...
android,unit-testing,gradle,robolectric
Fixed it: it seems like in the 0.11.+ build tool the exploded-aar folder is moved to root. I had to change the dependency: testLocalCompile fileTree(dir: "${rootDir}/build/exploded-aar", include: "**/classes.jar") ...
android,android-sqlite,robolectric
Reset all singleton instances between each test or you will get side effects like yours. @After public void finishComponentTesting() { resetSingleton(YourSQLiteOpenHelper.class, "sInstance"); } private void resetSingleton(Class clazz, String fieldName) { Field instance; try { instance = clazz.getDeclaredField(fieldName); instance.setAccessible(true); instance.set(null, null); } catch (Exception e) { throw new RuntimeException(); } }...
Libraries that contain internal resources (AKA aar) need to be mapped on project.properties files (at src/main). The file content will be something like this (if you are using Android Studio): android.library.reference.1=../app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/21.0.0 Take a look at the documentation....
The catch with Shadows is that they are meant for shadowing Android classes, not all classes. See documentation or even better, the code. Since AppUtils is not from the Android SDK, your shadow does not get picked up. This is not a flaw, as far as I can tell, it's...
android,testing,robolectric,robolectric-gradle-plugin
I wrote a step by step guide how to enable robolectric with android studio 1.1.0 http://nenick-android.blogspot.de/2015/02/android-studio-110-beta-4-and.html I guess you will find an answer for your issue. And an example https://github.com/nenick/AndroidStudioAndRobolectric
android,android-studio,gradle,robolectric
I found the problem. In my case it was not able to find the AndroidManifest.xml file & i haven't use customRobolectricTestRunner. Project Structure Gradle Version 2.2.1 Android Plugin Version 1.1.0 project build.gradle file's content buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.1.0' } } allprojects { repositories {...
This seems to be a proxy problem. When behind proxy you have to specify proxy settings for maven at {userHome}/.m2/settings.xml my settings.xml looks now like: <settings> <proxies> <proxy> <active>true</active> <host>proxy.host</host> <port>3128</port> </proxy> </proxies> </settings> below not working !! <settings> <proxies> <proxy> <id>proxy-https</id> <active>true</active> <protocol>https</protocol>...
android,gradle,android-gradle,robolectric,robolectric-gradle-plugin
This was confirmed as an issue with Robolectric 2.4 (Issue #1385). The issue has been closed by Erich Douglass today, with the following comment: We are working on appcompat support for 3.0. Until then, there's not much you can do. https://github.com/robolectric/robolectric/issues/1385 So until 3.0 is released, I will be using...
android,android-fragments,android-listfragment,robolectric
Turns out this is not robolectric's fault at all, but my own failure to understand mockito. I was able to fix this. It isn't robolectric's problem, but my own not understanding of the mockito framework. I think somehow it boils down to problem 2 in the Spy javadoc: Mockito does...
android,robolectric,google-analytics-v4
The solution of Akeem works for me. This is what I use for Robolectic 3.0, which has some changes: import org.robolectric.RuntimeEnvironment; import org.robolectric.Shadows; import org.robolectric.shadows.ShadowApplication; ShadowApplication shadowApplication = Shadows.shadowOf(RuntimeEnvironment.application); shadowApplication.declareActionUnbindable("com.google.android.gms.analytics.service.START"); ...
android,robolectric,pull-to-refresh
Please see responses from here: https://github.com/robolectric/robolectric/issues/1041 Change your pull to refresh initialization from onCreate into onStart or to onViewCreated (for fragment). Worked for me....
class,mocking,shadow,robolectric
I guess robolectric does not know that your class should be shadowed ;) Here is how to tell robolectric that you will shadow some not android sdk classes. public class MyRobolectricTestRunner extends RobolectricTestRunner { @Override protected ClassLoader createRobolectricClassLoader(Setup setup, SdkConfig sdkConfig) { return super.createRobolectricClassLoader(new ExtraShadows(setup), sdkConfig); } class ExtraShadows extends...