Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Junit Testing vs. Normal Junit Testing

I'm taking over an android project and I wish to introduce unit tests to the project, to help avoid possible regressions.

For normal java projects, I have two source folders: src and test. The src source folder contains all of my source files and my test source folder contains all of my unit tests, which I believe is pretty standard for keeping test separate from the source, so you don't have to ship with them.

I've been doing some reading online and the approach with android apps looks to be a little bit different. Several examples talk about setting up a 2nd project for an android test project and then referencing the android project.

I wish to confirm a few things:

  1. Is having a 2nd project for the testing the appropriate thing to do when it comes to testing android projects or am I just finding bad examples?
  2. Should all unit tests be android unit tests? E.g. Yes they should all be, or no I should mix between android unit tests and junit because junits have less overhead.
  3. What additional benefits do android unit tests give over junit tests? E.g. Handles to the emulator, etc.
like image 758
James Oravec Avatar asked Apr 16 '13 18:04

James Oravec


People also ask

What is JUnit testing Android?

JUnit is a “Unit Testing” framework for Java Applications which is already included by default in android studio. It is an automation framework for Unit as well as UI Testing. It contains annotations such as @Test, @Before, @After, etc.

What is difference between unit testing and JUnit?

It supports the test to run by writing and testing along. JUnit framework was initially based on the SUnit framework which is used for Unit testing but then later it was updated with Java using Selenium WebDriver. JUnit is now is used as a standard when we need to perform testing in Java.

What is JUnit testing explain different types of testing?

JUnit is a unit testing open-source framework for the Java programming language. Java Developers use this framework to write and execute automated tests. In Java, there are test cases that have to be re-executed every time a new code is added. This is done to make sure that nothing in the code is broken.

Why do we use Android JUnit runner?

The AndroidJUnitRunner class is a JUnit test runner that lets you run instrumented JUnit 4 tests on Android devices, including those using the Espresso, UI Automator, and Compose testing frameworks.


1 Answers

Is having a 2nd project for the testing the appropriate thing to do when it comes to testing android projects or am I just finding bad examples?

Yes, it's typical to have a separate "test project" for testing Android specific code. http://developer.android.com/tools/testing/testing_android.html

Should all unit tests be android unit tests? E.g. Yes they should all be, or no I should mix between android unit tests and junit because junits have less overhead.

You'll usually have a mix because you can't test Android specific code on a standard JVM with regular ole' JUnit (not without some helper libraries, more on that in a moment).

In practice I've found that it makes sense to divide your application into plain JVM components and Android portions. For instance, if you need to communicate with a REST API you can have a separate component that does only this and which is plain Java. These types of components can easily be tested with standard JUnit. This type of architecture also leads to a clearer separation of responsibility and often and easier to understand and maintain design as well. (Such components can be included in your Android app as regular JARs.)

What additional benefits do android unit tests give over junit tests? E.g. Handles to the emulator, etc.

Android testing can be slow and painful because a full Android test runs the Android stack in an emulator (or on a device). Android tests are however necessary for testing the parts of your application that are Android specific, such as Context/Activity/Service and so on.

Because of the cumbersome and slow nature of native Android tests several frameworks have been created that mock or stub parts of the SDK and take different approaches to help. You may want to look into Robolectric and Robotium, for instance. (These each have their own pros and cons.)

like image 184
Charlie Collins Avatar answered Oct 09 '22 23:10

Charlie Collins