• Stars
    star
    128
  • Rank 281,044 (Top 6 %)
  • Language
    Java
  • License
    GNU General Publi...
  • Created almost 10 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A template Gradle / Android project which integrates and configures Robolectric, Robotium, JUnit4 and standard Android Instrumentation together in one project.

#Android All-test Gradle Sample

This project provides a Gradle project template for an Android project which integrates and configures testing using Robolectric, Robotium, Java JUnit Tests and Android Instrumentation tests together in one project.

Background

I develop the TripComputer Android App but I find testing it using the standard Instrumentation framework is really slow because it demands either a connected device or an emulator to be running upon which it runs the tests.

Slow testing cycles are a disincentive to TDD, so most tutorials bestow the virtues of switching to the Robolectric framework when unit testing Android apps because it 'simulates' the android SDK in a standard Java VM. This allows your Android tests to be executed outside of the emulator/device. It also allows the use of JUnit v4 syntax rather than the older JUnit v3 required by the built in Android Instrumentation testing framework.

However, there's a problem - it's tricky to get this stuff to work in Android Studio.

I've been an Android Studio user ever since it first went public nearly 2 years ago. It's an awesome IDE but one consequence of it's use is that it promotes the Gradle build system to be the default choice for Android projects. This is good news for Android developers but unfortunately, getting Android Studio, Gradle, Robolectric and JUnit to all work happily together is a real pain in the rear.

Over the past year or so it's been a slowly improving picture, but now Android Studio has gone to a 1.0 release, I (and many others) have figured the time was right to try and bring these tools together.

The android-alltest-gradle-sample project on GitHub is my attempt to create a template project that can be used as a starting point for anyone who wishes to use these best of breed Android Testing tools together with Gradle and Android Studio in one project.

To use the sample, simply clone the repository (or download a ZIP), import into Android Studio, test it and then start running code. Check out the Acknowledgements section in the readme for further help, tips and advice (including how to execute your Robolectric tests from within Android Studio as well as from the cmdline).

Project Structure

Project is broken down into 3 modules

  1. /core - Vanilla Java library module (with JUnit 4 tests)
  2. /app - Android app and integration test module (Robotium and Instrumentation)
  3. /app-tests - Android unit test module (Robolectric)

Dependencies & Frameworks

This project template integrates and depends on the following popular Java and Android frameworks:

  1. AssertJ for Android. Used to make the testing of android components easier by introducing an android specific DSL. See https://github.com/square/assertj-android

  2. Robolectric. Used for the simulated testing of Android apps (i.e. device API's are simulated, so there is no need for an emulator or physical device). See https://github.com/robolectric/robolectric

  3. JUnit. Used to simplify testing of core Java and simulated Android tests. See http://junit.org/

  4. AppCompat. Popular google support library for backwards compatibility. See https://developer.android.com/tools/support-library/features.html

  5. Robotium. Used to augment normal Instrumentation Tests and provide black box integration testing. https://code.google.com/p/robotium/

Running Tests

Tests can be run using one of the following (or combinations thereof):

./gradlew clean build connectedCheck

Known Issues

Robolectric isn't fully suporting AppCompat v21 yet, and strange random failures can affect the smooth running of the build if you choose to 'clean' it. There is more information on Stackoverflow here. The suggested workaround is to separate the gradle commands as follows...

./gradlew clean
./gradlew assemble
./gradlew test
./gradlew connectedCheck

Cautions

This stuff is brittle! It's a very volatile set of tools we're using.

  1. Android Studio is only just out of beta.
  2. Android build tools and Gradle plugin is only just out of beta.
  3. Android app development borrows the Java language syntax, but not the runtime, so the compile & build tools are complex compared to standard Java. Kind of reminds me of the early days of JEE...
  4. Gradle is still relatively new to me, so probably this project can be much improved.
  5. Testing with standard Android is way harder and clunkier than it should be IMHO.
  6. It's very slow (on my Core 2 duo anyway, your mileage may vary).
  7. I'm not what you'd call a 'natural born tester' (but I do at least try).

I'm not sure anyone other than Google can improve the situation regarding Android testing if I'm brutally honest. It's a bit of a 'dogs dinner'.

On the plus side, I haven't used any additional gradle plugins. I've seen and tried lots but they can go out of date quite quickly.

Acknowledgements

The original inspiration for this template project came from Jason Atwood's blog at Big Nerd Ranch:

http://www.bignerdranch.com/blog/all-in-together-android-studio-gradle-and-robolectric/

This template project is based on the following blog post and code samples (but I further further adapted it soa as to include instrumentation and a java core module)

http://blog.blundell-apps.com/android-gradle-app-with-robolectric-junit-tests/

The tests for Robolectric can also be configured to run from within Android Studio 1.0.x if you follow this post:

http://blog.blundell-apps.com/how-to-run-robolectric-junit-tests-in-android-studio/

I had lots of issues with AppCompat which took a while to fix, but @sjorsgeelen and @loeschg had the right answer here:

robolectric/robolectric#1334

About the Author

Ben Wilcock is the developer of TripComputer , the only distance tracking app for Android with a battery-saving LOW POWER mode. It’s perfect for cyclists, runners, walkers, hand-gliders, pilots and drivers. It’s free! Download it from the Google Play Store now:-

Get Trip Computer on Google Play

You can follow Ben on his Blog, Website, Twitter or LinkedIn

More Repositories

1

cqrs-microservice-sampler

A Java Microservice & CQRS Demo using Spring Boot, Axon & Docker
Java
652
star
2

spring-cloud-gateway-demo

Code and articles to help folks get started with Spring Cloud Gateway.
Java
181
star
3

spring-rsocket-demo

Getting Started With RSocket in Spring Boot
Java
147
star
4

spring-cloud-stream-demo

Simple Event Driven Microservices with Spring Cloud Stream
Java
98
star
5

pcf-axon-cqrs-demo

Demonstrates CQRS and Event Sourcing microservices in Java using Pivotal CloudFoundry, SpringBoot and the Axon Framework.
Java
52
star
6

axon-initializr

A customised Spring Initailizr to help folks create Axon based CQRS & Event Sourced applications from scratch.
Java
52
star
7

backstage-plugin-backchat

A quick and dirty frontend plugin that integrates a GenAI feature into Backstage
TypeScript
47
star
8

springone-2020-rsocket-talk

Getting started with RSocket
Java
31
star
9

pcf-spring-cloud-services-demo

Demonstrates how to write Spring Boot applications that use the PCF Spring Cloud Services (Hystrix, Eureka and Config)
Java
25
star
10

android-couchbase-dagger-robolectric

This Android / Gradle project integrates Couchbase, Robolectric and Dagger so that unit testing can occur without the need for a connected device or emulator.
Java
17
star
11

spring-cloud-dataflow-demo

Demonstration of streaming applications using Spring Cloud Data Flow with microservices written in Spring Cloud Stream.
Java
12
star
12

spring-cloud-contracts

Demonstrates the use of Spring Cloud Contracts for Consumer Driven API's
Java
12
star
13

cf-cqrs-microservice-sampler

CQRS Microservice Demo that runs and scales on CloudFoundry.
Java
9
star
14

backstagecon-2023

BackstageCon 2023
6
star
15

axon-cqrs-sample

Java
6
star
16

test-driven-jee-modernisation

An example of how ArchUnit can be used to help you eradicate dependencies to the Java Enterprise framework.
Java
3
star
17

python-cloudfoundry-demo

Python
2
star
18

pcf-route-service

Demonstrates a simple PCF Route Service that injects a new HTTP header "x-auth-user" containing a signed JWT token.
Java
2
star
19

tanzu-application-platform-scripts

Shell
2
star
20

spring-cloud-connectors-demo

Demonstrates connecting to services hosted in Cloud Foundry from Spring Boot using spring-cloud-connectors.
Java
2
star
21

tap-demo-config-files

tap demo configuration files
2
star
22

test-java-002

Java
1
star
23

microservice-sampler-config

configuration used by the config server for the microservice-sampler microservices.
1
star
24

python-pipenv

Simple Python App using PipEnv package management
HTML
1
star
25

spring-oauth-sso-demo

Demonstrates the use of OAUTH2 with SpringBoot.
Shell
1
star
26

backstage-catalogs

backstage-catalogs
1
star
27

axon-jpa-example

A simple demo of Queries backed by JPA based "projections" using Axon 4.0
Java
1
star
28

springboot-djl-demo

Demonstrates ML based sentiment analysis via REST API.
Java
1
star
29

akka-cqrs-sample

Java
1
star
30

systems-techdocs

The repo contains some sample Backstage TechDocs documentation for the Where For Dinner sample application.
Shell
1
star
31

TripComputer-Android-Project

TripComputer is an Android App that displays time, distance, cost and many more facts about your journey.
Java
1
star