robolectric

Using assets in robolectric tests

 ̄綄美尐妖づ 提交于 2019-11-30 17:38:19
In my robolectric tests I am trying to read a file. Since this file contains just the test data I have put it in assets of the tests. The directory structure looks like this. src/main/assets/prod_data_file src/test/assets/test_data_file However when I try to read the file by getShadowApplication().getAssets().open(MY_FILE_PATH) it gives me FileNotFoundException . Does anybody know how to fix this? Since my tests are just reading these files and returning file data as a String I was able to do that via getClass().getClassLoader().getResourceAsStream(MY_FILE_PATH) and then read file. Dont really

Can't run Roboelectric test

∥☆過路亽.° 提交于 2019-11-30 17:18:44
问题 I keep on getting java.lang.NoClassDefFoundError: android/content/pm/PackageManager$NameNotFoundException ... java.lang.ClassNotFoundException: couldn't load android.content.pm.PackageManager$NameNotFoundException... Caused by: java.util.zip.ZipException: invalid LOC header (bad signature) Any idea what causes this and how to get it fixed? 回答1: your jar file may be corrupted so re add the jar file or rebuild the path of jar file ... clean the project and re run it. 回答2: You need install

JaCoCo doesn't work with Robolectric tests

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 16:29:01
问题 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() } dependencies { classpath 'com.android.tools.build:gradle:2.0.0-beta6' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } } allprojects { repositories { jcenter() maven { url "https://oss.sonatype.org/content/repositories/snapshots" } } } task clean(type:

Usage of MultiDexApplication causes Robolectric test for the Application class to break

笑着哭i 提交于 2019-11-30 16:28:11
问题 Adding Multi dex support with the support v4-r21 using gradle def (https://plus.google.com/+IanLake/posts/JW9x4pcB1rj) apply plugin: 'com.android.application' android { compileSdkVersion 19 buildToolsVersion "20.0.0" defaultConfig { applicationId "info.osom.multidex" minSdkVersion 19 targetSdkVersion 19 versionCode 1 versionName "1.0" } buildTypes { release { runProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } dexOptions { preDexLibraries =

Android Robolectric Click RecyclerView Item

ぐ巨炮叔叔 提交于 2019-11-30 14:44:43
问题 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 findViewHolderForPosition is always null . The adapter returns a non-0 number from getItemCount() (there are definitely items in the adapter). I'm using Robolectric 2.4 SNAPSHOT. 回答1: Seems like the issue was that RecyclerView needs to be measured and layed

Robolectric tanks on Application objects that load JNI libraries. Can I get a workaround?

大城市里の小女人 提交于 2019-11-30 13:49:05
The Application object for my Android app loads a JNI library, and Robolectric doesn't seem to like that. When I go to run my tests Robolectric craps out and I get this stack trace: java.lang.UnsatisfiedLinkError: no cperryinc-jni in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1758) at java.lang.Runtime.loadLibrary0(Runtime.java:823) at java.lang.System.loadLibrary(System.java:1045) at com.cperryinc.application.MoolaApplication.(MoolaApplication.java:24) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at com.xtremelabs

Mock class in class under test

Deadly 提交于 2019-11-30 13:15:42
问题 How I can mock with Mockito other classes in my class which is under test? For example: MyClass.java class MyClass { public boolean performAnything() { AnythingPerformerClass clazz = new AnythingPerformerClass(); return clazz.doSomething(); } } AnythingPerformerClass.java class AnythingPerformerClass { public boolean doSomething() { //very very complex logic return result; } } And test: @Test public void testPerformAnything() throws Exception { MyClass clazz = new MyClass(); Assert.assertTrue

ClassCastException exception when running Robolectric test with Power Mock on multiple files

拜拜、爱过 提交于 2019-11-30 12:46:46
So I set up the power mock based on the reference guide here . It all seems to run perfectly fine with a single test class. But when executing multiple JUnit tests I am getting the following error on the second test class. As you can see from the stacktrace below I am trying to mock a otto Bus instance. It seemed to mock properly on the first test class but on the the second class I am getting this class cast exception. On the stacktrace I am getting suggestion to disable Objenisis cache but I am not sure how to accomplish that and if that is actually the root cause as I am using classloading

How do I create a shadow class to work around crashes related to custom attributes when testing an Android app with Robolectric?

浪尽此生 提交于 2019-11-30 12:45:56
I'm integrating a custom widget into my project. It uses custom attributes and is crashing Robolectric. From what I gather, custom attributes aren't supported yet . Note: the constructor is "hostile" because it throws exceptions on construction, but I've commented them out for now. Crash Log java.lang.RuntimeException: error inflating layout/main at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:106) at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:82) at com.xtremelabs.robolectric.res.ViewLoader.inflateView(ViewLoader.java:86) at com.xtremelabs

Robolectric vs Android Test Framework

落爺英雄遲暮 提交于 2019-11-30 11:16:59
问题 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 DalvikVM, making it faster than Android framework. Are there any other major benefits that stand out ? 来源: https://stackoverflow.com/questions/18271474/robolectric-vs-android-test-framework