RuntimeEnvironment.application in Robolectric tests after AndroidStudio 2.3 update - android

We are using Robolectric for tests in Android Studio.
After a recent update from 2.2.x to 2.3 all my tests run with Robolectric (i.e. #RunWith(RobolectricTestRunner.class)) fail.
More specifically, I'm getting a ClassCastException here, as it seems that RuntimeEnvironment.application is no longer returning the custom application type created for test purposes.
MyTestApplication testApp = (MyTestApplication) RuntimeEnvironment.application;
// do something with testApp
Apparently I'm getting a plain old android.app.Application instead of the expected type...
The crux:
Neither tests, nor config were changed, the only thing I did was the AndroidStudio update (I did git reset --hard just to make sure...).
On the command line my tests run just fine, which makes me think this might be an issue with the test runner in Android Studio.
:(
I think I tried just about everything in Android Studio, like clean/rebuild, resync gradle files, invalid cache + restart... I tried to re-download and reinstall Android Studio (2.2.3 and 2.3 versions). I cleaned my local gradle cache and my local .m2 repo just to make sure, but to no avail... :(

Found the solution:
Go to Run > Edit Configuration
then in the Run/Debug Configurations for JUnit > java in app
Add $MODULE_DIR$ in the field Working directory.

Related

Instrumented test is stuck on "Connected to process"

When I try to run the ExampleInstrumentedTest on my Android Studio project, it builds fine, installs the test and then it is stuck on "Connected to process ####..." and no test results are displayed.
I've waited for more than 2 minutes and still no result. When I create a new Android Studio project with Empty Activity, the test runs fine and results are displayed. It's not working unfortunately for my existing project which I had been working on for a long time but never tried to run the Example test.
Things I've tried:
Restart Physical device
Uninstall app
Invalidate Caches and Restart
Clean Project
Here's the screenshot of how it looks like:
I don't know how I solved this problem but these are the changes I made:
I changed the Kotlin version from 1.6 to 1.7.
I removed an unnecessary test file Navigation.
I removed all the unnecessary testing dependencies.
And, finally I clean built the project and voila. It works.

Android Studio 3.2 empty test suite

I have created a new project in Android Studio 3.2. Without making any changes I am not able to run all the unit tests in the group. I receive and error saying No tests were found
I am attempting to run the tests by right clicking on the group and clicking Run Tests.
I can run the unit tests if I open up the file and click on the run button next to the class declaration. I can also run the unit tests if I open the project in Android Studio 3.1.4. I can right click on the group and run all the unit tests with no error.
Looking at this bug report, I think it should be fixed in 3.2.1: https://issuetracker.google.com/issues/115708445#comment12
After spending an entire day trying resolve this while writing some unit tests I found one workaround that works for my project.
Basically what my workaround is to add Build to the Before launch options.
After adding this option I'll occasionally get the No tests found error message but simply rerunning the test worked every time after adding that setting.
I'm not sure if it will help with everyone's problem but it seems to have mitigated the issue with my project. Hopefully this works for someone else as well.
The workaround until the bug is fixed is running tests in terminal, just type:
./gradlew test
or
./gradlew testDebugUnitTest. If you are using Windows replace ./gradlew with gradlew.bat. You can also download Android Studio 3.3 Canary from here https://developer.android.com/studio/preview/ – there aren't problems with running tests via this version.

Gradle + Robotium + Jenkins

I am trying to set up a CI environment with Jenkins and Robotium. I want to use the same project for both built and test, but seems so tricky to get all working. I was wondering if someone had something like that working and if it can publish at least build.gradle and the project structure. Thanks.
Have been running in production for a few months now. See this question for a sample project and video of how to use robotium with gradle.
https://stackoverflow.com/a/23295849/1965124
As for the jenkins side of things:
Install the android sdk on the machine that will be running jenkins
set up android home variable
install the android plugin
run the following tasks from inside a jenkins job: clean connectedAndroidTest
after running 1 build (it will fail the first time), change the local.properties file to point to the local installation of the android sdk.
Let me know if you need any more pointers, its not as hard as I initially thought it would be.
I configured TeamCity as CI server. Also, project builds by Gradle.
The main idea is to execute gradle connectedInstrumentTest, that task will execute all project's tests on all connected devices, then it will put the test results in standard ant-junit format, so then you can set Jenkins to parse app-folder/build/instrumentTest-results/connected/*.xml test results.
If you got more questions, you can post them to the comments.

android-maven-plugin: deploy method that does not delete SharedPreferences

I built an android project that set up with android-maven-plugin. When I execute android:deploy and run the program, SharedPreferences always initialized.
Is there a way to deploy my project to AVD without deleting SharedPreferences storage?
P.S.
I use Intellij IDEA and noticed that its native android run/debug support does not delete SharedPreferences. However, after I add scala support on my project, IDE reports "Too many methods: 112423; max is 65536" error when I run the application. Perhaps it is due to lack of proguard preprocessing. If there are a way to apply proguard before run the android app with Intellij IDEA android support, it would be an equally effective solution.
I think your app is being uninstalled before being reinstalled again - this would cause you to lose your SharedPreferences values. This can be controlled with the Android Maven plugin parameter undeployBeforeDeploy.
I just tried to create a Maven project with the android-quickstart archetype and found that it automatically added the following line to my POM:
<undeployBeforeDeploy>true</undeployBeforeDeploy>
Try to set this to false and try again.
If you have it set to true, it prints this line in the console when you run mvn android:deploy:
[INFO] Successfully uninstalled [package] from [device]

Tests run via maven, and adb, but not IntelliJ - why?

I'm working on a project defined with maven, and I can successfully run the maven script to build the app, and run automated (junit 3.8.2) tests on the device with instrumentation.
Likewise I can use adb to run the instrumentation tests successfully.
However running the instrumentation tests from IntelliJ is failing with this:
Running tests
Test running startedTest running failed: Instrumentation run failed due to 'java.lang.ClassNotFoundException'
Empty test suite.
Logcat shows the not found class as org.junit.Test. The IntelliJ project was created by importing the root pom, and otherwise works fine. I just had to create a Run Configuration for the tests, specifying the instrumentation runner.
I've tried on several devices and they all provide the same result.
I would expect the junit Test class to be in the OS framework on the device, so I'm confused as to why it's not being found. I've tried setting the junit dependency in IntelliJ from Provided to Compile, but that makes no difference.
What have I missed?
Okay this is an old question, however, most likely you'll have different classpath in maven as compared to intelliJ. You can solve this pretty easily by going to the intelliJ project Structure ( File --> Project Structure or Ctrl + Alt + Shift + S).
Under Project Settings, click Libraries and manually add the library that is missing.
I do find it strange though that IntelliJ is not picking up maven dependencies. After all, if maven runs it, so should intelliJ - it has the best maven integration available in an IDE and VERY VERY much better than that of eclipse. So your issue does make me feel something else is the root cause...
Perhaps you can ask the IntelliJ people?
More info on your part could allow us to give better feedback...

Categories

Resources