Book Image

Learning Android Application Testing

Book Image

Learning Android Application Testing

Overview of this book

Table of Contents (16 chapters)
Learning Android Application Testing
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Android unit tests


There are some cases where you really need to test parts of the application in isolation with little connection to the underlying system. In Android, the system is the Activity framework. In such cases, we have to select a base class that is high enough in the test hierarchy to remove some of the dependencies but not high enough for us to be responsible for some of the basic infrastructure of instantiating Context, for example.

In such cases, the candidate base class is AndroidTestCase because this allows the use of Context and Resources without thinking about Activities:

public class AccessPrivateDataTest extends AndroidTestCase {

   public void testAccessAnotherAppsPrivateDataIsNotPossible()  {
        String filesDirectory = getContext().getFilesDir().getPath();
        String privateFilePath = filesDirectory + 
"/data/com.android.cts.appwithdata/private_file.txt";
        try {
            new FileInputStream(privateFilePath);
            fail("Was able to access another...