robolectric

How do I know if my app is running with Robolectric?

允我心安 提交于 2019-11-27 06:39:23
问题 I have an android app that uses ORMLite/SQLite and I use Robolectric in conjunction with JUnit 4 to allow me to run unit tests in Android Studio and on a Jenkins build server. Typically I would setup test data in my tests, in the setup, and then run my test scenarios against it but when I tried to do this I started getting issues and exceptions which seemed to be related to files being locked or something and that seems to be a problem others have had... so what I have done up until now is

Confused about testCompile and androidTestCompile in Android Gradle

核能气质少年 提交于 2019-11-27 06:21:47
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 others use androidTestCompile . Certainly both can't be valid? Can somebody explain the difference between the both and which of these should be the one used when using Robolectric? Simply testCompile is the configuration for unit tests (those located in src/test ) and androidTestCompile is used

How does exactly custom Shadow objects work in Robolectric?

喜你入骨 提交于 2019-11-27 05:47:26
问题 If I write a custom Shadow for my Activity, and registering it with RobolectricTestRunner, will the framework intercept the Activity with my custom Shadow whenever it's started? Thanks. 回答1: The short answer is no. Robolectric is selective about what classes it intercepts and instruments. At the time of this writing, the only classes that will be instrumented must have a fully qualified classname match one of these selectors: android.* com.google.android.maps.* org.apache.http.impl.client

How can we access context of an application in Robolectric?

℡╲_俬逩灬. 提交于 2019-11-27 05:41:23
问题 Actually, I need to get a response of an API call, for that I required Context . 回答1: Update. Just use for version 1.x and 2.x: Robolectric.application; And for version 3.x: RuntimeEnvironment.application; And for version 4.x: add to your build.gradle file: testImplementation 'androidx.test:core:1.0.0' retrieve the context with: ApplicationProvider.getApplicationContext() 回答2: You can use RuntimeEnvironment.application 回答3: Use this: Robolectric.application 回答4: Add testImplementation

How to debug when running Robolectric tests in Android Studio?

浪子不回头ぞ 提交于 2019-11-27 05:35:09
问题 I need to run debug while my tests execution in Android Studio + Robolectric. Each time I try to run them by selecting debug for the test task from Gradle tasks I get the error message: Error running package_name:app_name [test]: Unable to open debugger port : java.net.SocketException "Socket closed" Any ideas? 回答1: I found the reason of the error. To make it run you need to uncheck the "Use in-process build" option in Compiler -> Gradle settings of Android Studio. 回答2: I was getting this

How to run unit tests with Android Studio

懵懂的女人 提交于 2019-11-27 04:20:40
问题 I'm using Jake's Android unit tests plugin for gradle: https://github.com/JakeWharton/gradle-android-test-plugin My build.gradle looks like this: dependencies { // analytics compile('com.crittercism:crittercism-android:3.0.11') // retrofit compile('com.squareup.retrofit:retrofit:1.2.2') compile('com.squareup.okhttp:okhttp:1.2.1') // dagger compile('com.squareup.dagger:dagger:1.1.0') compile('com.squareup.dagger:dagger-compiler:1.1.0') // compatibility compile('android.compatibility:android

Is it possible to do real http requests with robolectric

跟風遠走 提交于 2019-11-27 02:19:55
问题 The issue I have with robolectric is that it doesn't make real HttpRequests. Could I replace the HttpClient implementation of robolectric using a real one actually making the calls to my local test server? 回答1: Jan Berkel submitted a pull request to Robolectric that was merged into master on March 8th. You can now call Robolectric.getFakeHttpLayer().interceptHttpRequests(false); in the beginning of your test to bypass the mocked http layer in Robolectric. You can download the jar directly

Android project with Robolectric and Gradle (Android studio)

丶灬走出姿态 提交于 2019-11-27 01:36:32
问题 I'm trying to use Robolectric in a project build with gradle inside the new Ide for android: Android studio, but I'm facing a strange problem, I've correctly imported all the libraries and created the "test" folder inside "src", the fact is that whenever I run the tests the ide keep saying "Class not found: "com.example.myandroidproject.test" what I'm doing wrong? i need to change something in the gradle.build? here's my directory structure: 回答1: This is unlikely to work out of the box as src

Robolectric with Gradle: Resources not found

和自甴很熟 提交于 2019-11-26 23:53:58
问题 I'm trying to run my Robolectric tests together with the new Gradle Android build system, but I'm stuck at accessing the resources of my main project. I split the build into two separate projects to avoid conflicts between the java and the android gradle plugins, so the directory structure looks roughly like this: . ├── build.gradle ├── settings.gradle ├── mainproject │ ├── build │ │ ├── classes │ │ │ └── debug │ ├── build.gradle │ └── src │ └── main │ ├── AndroidManifest.xml │ └── ... └──

How can I test fragments with Robolectric?

◇◆丶佛笑我妖孽 提交于 2019-11-26 17:59:12
问题 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) activity.findViewById(R.id.container), null); myFragment.onAttach(activity); myFragment.onActivityCreated(null); I'm working with API level 13 (Honeycomb). Thanks. 回答1: Edit #4 & #5 : In Robolectric 3.*, they split up the fragment starting functions