JUnit and Android Studio

To have installed Android Studio 1.0.1 or up.

Installing JUnit in Android Studio

1. Over your project created open the build.gradle file and add

To dependencies:
// Apps dependencies, including test
compile 'com.android.support:support-annotations:21.0.3'
// Testing-only dependencies
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'

To defaultConfig:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

To packagingOpptions:
Fig 1. Example build.gradle file.

2. A little about JUnit for Android
In the framework JUnit for Android you can make test cases with the next classes:

ApplicationTestCase: Verifies the Application classes in a controlled environment.
AndroidTestCase: It contains methods for accessing resources like Activity Context.
IntrumentationTestCase: Verifies a particular feature or behavior of target application, e.g. verify UI output of application.

And a set of classes for component-specified test case like:

Activity Testing
ActivityTestCase class, used to test UI interaction like key event, touch event.

Content Provider Testing
ProviderTestCase class, used to test store and retrieve data and make it accessible across applications.

Service Testing
ServiceTestCase class, used to test various states of Services life cycle and interaction between application and service.

3. Mocks Object Classes
Additionally, Android provides classes that create mock system objects like:
Context Object, ContentProvider Object, ContentResolver Object, Service Object, Intent Object. These mocks facilitate dependency injection for testing.

4. Let's do it!
We are going to do two test case very useful, GPSTest and ConnectivityTest. Both classes extend from AndroidTestCase class, this kind of testing it provides methods for testing permissions.

GpsLocationTest uses the LocationManager class and verifies that the GPS is available and the app had the permissions.

Fig 2. GpsLocationTest  class.

ConnectivityTest uses the ConnectivityManager class to verified the accesses to network and the HTTP connectivity.

Fig 3. ConnectivityTest  class.

You have also an example that ApplicationTest:

Fig 4. ApplicationTest class.

4. Setting up and Running the Test case created.
Choose your test case crated and click over Run option, then choose your device connected and automatically the test is going to run. Please, to see for more detail the article Espresso con Android Studio.

You can download the example project in GitHub.
Good travel.
Thanks for your comment