android-studio-testing-project-template


Source link: https://github.com/emmano/android-studio-testing-project-template

android-studio-testing-project-template

The goal of this project is to speed up the setup of Robolectric, Robotuim, Double Espresso, Roboguice, and Mockito (along with some other nice libraries) in Android Studio. Since there is not built in support for Robolectric (just yet, hopefully) there is a lot of manual set up that has to be done. This project uses jeske717's Robolectric-gralde plugin as well as his InjectedTestRunner (more about it later) found on Java Center.

How is this different than the Deckard-Gradle template provided by Robolectric?

Deckard requires you to download their template and then customize things like the name of your project, module name, etc. This template is configured with the information provided when you go through the project creation flow.

Tested on:

  • Mac OSX (Mavericks)
  • Android Studio 0.8.1, 0.8.5 and 1.0

Set Up

  1. Clone the repo
  2. Go to your Android Studio installation fodler, and then to plugins/android/lib/templates/gradle-projects
  3. Create a backup of the NewAndroidProject and NewProjectTemplate folders (just in case you want to revert the changes)
  4. Copy the two folders you just cloned into the plugins/android/lib/templates/gradle-projects
  5. Open Android Studio and create a new project
  6. After build.gradle gets executed by Android Studio, you would get a dialog saying that the *.iml file was updated and the project needs to be reloaded. Click on OK. (if the dialog doesn't show automatically you should manually run the addRobolectricTestSourcesToIml Gradle task (see below on how to run Gradle tasks)).
  7. Upon restart the robolectricTest/java directory should be green. Now you need to run the configureJUnitDefaultToUseRobolectricClasspath Gradle task. To do so, go to View-> Tool Windows->Gradle. Expand the menu that has the name of your app, and double click on the configureJUnitDefaultToUseRobolectricClasspath task. Android studio will prompt the same message as before (requesting to reload the project). Click OK.
  8. After Android Studio reloads, you should syncronize your project. File->Syncrhonize.
  9. Run the MyActivityRobolectricTest.java inside the robolectricTest module. Right click on the method, Run>testMethodName() (the second option on the dropdown; the JUnit one).
  10. Go to Android Studio > Preferences > Compiler > uncheck "Use in-process build"
  11. As part of the Espresso configuration a custom Run Configuration is needed. They show how to do it on their docs here (look at the Android Studio picture). Make sure you check the "Show chooser dialog". It defaults to Emulator.
  12. If everything is set up correclty, the test should run and pass.
  13. Apparently Android Studio 1.0 defaults to ActionBarActivity regardless of your minSdkVersion selection. Make sure your MainActivity extends Activity or that you fix the Instrumentation tests to run on older versions of Android.

InjectedTestRunner

InjectedTestRunner is the "glue" between Mockito and Roboguice. It basically looks for all @Mock on the test and automagically(thanks to jeske717) binds the mock instance to the correct @Inject on the implementation class. For more information go here

Known Limitations (We will try to fix)

  1. You might encounter some problems when trying to add a new package under robolectricTest/java/your.app.package
  2. After you create a new test on a class that already contains tests, sometimes you will need to run the tests twice if you are running all the tests on the class. Synchronizing the project also works. It seems that gralde doesn't pick up the new added test on the first run. It works on the second run. (Step 10 above should fix this)
  3. Make sure you create the project setting KitKat as minSdk to begin with. Tried using a different minSdk during the project creation flow and it broke the template. You can change your minSdk after you follow the steps above.
  4. Specifying to use the GoogleInstrumentationTestRunner in build.gradle, as required by Espresso, makes the whole test suite run. Removing testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" from build.gradle, allows to run Robotium tests independently, but Espresso tests will not run. I will try to find a way to separate the runs.
  5. In order to see the robolectricTest folder, you need to switch Android Studio from the Android View to the Project View

TODO

  1. Be able to run Robotium and Espresso tests independently

Resources

Navigation Drawer Activity with Google design style and simplified methods.

ListView in Android supports header and footer views - views that do not belong to the underlying adapter but otherwise show up in the list and scroll along with the contents. However, they only work if you have not yet set your own adapter and are therefore not terribly flexible.

The SackOfViewsAdapter is another way of approaching this. Here, you provide the Views that make up the rows, and the adapter feeds them to Android as if they were newly created.

The SackOfViewsAdapter is designed to be sub-classed, mostly to determine how isEnabled() behaves, so you can control which of those views are selectable and which simply scroll with the list.

StrictMode is a handy feature in API Level 9 and higher, telling you where your Android application is doing things it probably should not on the main application thread.

In the spirit of StrictMode, the StrictModeEx project offers classes to help you diagnose similar sorts of problems beyond what StrictMode itself offers.

Right now, that consists of one class: StrictAdapter. This ListAdapter wrapper will log slow-running getView() calls, plus optionally give you an overall performance view on how your Adapter is doing in the code you control.

geo

Java utility methods for geohashing.

A very, very compact library that enables you to create on-demand singletons within your application and easily store them to disk. Utilizing a dead-simple API, this library makes creating singletons and persisting data much more fun!

This app is built during the free time of the developer for fun. It provides with a tool to test some Intent behavior while building and testing other apps or just for fun playing with the framework. ;) This app would not work and feel the same way if it weren't for some great Android open-source projects that were used during the development.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes