Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9187463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:46:28+00:00 2026-06-17T19:46:28+00:00

I am using InstrumentationTestCase to unit test a component of my application. The component

  • 0

I am using InstrumentationTestCase to unit test a component of my application.

The component persists data to the internal storage and uses Context::fileList(); to retrieve the persisted files.

I experience the following problem: Using this method in the app (on the device) works perfectly fine. But when I try to (Android-)Unit-Test (also on the Device) with use of InstrumentationTestCase I get a NullPointerException inside the fileList() method. I digged into the android source and found out that getFilesDir() (see source here) returns null and causes this error.

The code to reproduce is the following:

public class MyTestCase extends InstrumentationTestCase
{   
    public void testExample() throws Exception
    {
        assertNotNull(getInstrumentation().getContext().getFilesDir()); // Fails
    }
}

My questions are: Is this behaviour intended? What can I do to circumvent this issue? Am I using InstrumentationTestCase right or should I use something different?

I found this question but I’m not sure if this covers the same problem I have.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:46:30+00:00Added an answer on June 17, 2026 at 7:46 pm

    I think that you are right with keeping your test data separate from tested application.

    You can fix problem with Null by creating files directory for Instrumentation app by executing the following commands

    adb shell
    cd /data/data/<package_id_of_instrumentation_app>
    mkdir files
    

    You can do above only on emulator or rooted device.

    Then test from your question will not fail. I did it and also uploaded file named tst.txt to files dir, all below tests were successful:

    assertNotNull(getInstrumentation().getContext().getFilesDir());
    assertNotNull(getInstrumentation().getContext().openFileInput("tst.txt"));
    assertNotNull(getInstrumentation().getContext().openFileOutput("out.txt", Context.MODE_PRIVATE));
    

    But I think more convenient way to provide data to test project is to use assets of test project where you can simply save some files and open them:

    assertNotNull(getInstrumentation().getContext().getAssets().open("asset.txt"));
    

    or if you want to save some results of tests to the file you can use ExternalStorage:

    File extStorage = Environment.getExternalStorageDirectory();
    assertNotNull(extStorage);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using the InstrumentationTestCase class in order to unit test some things within
I am trying to test drive an application feature using Robotium. One of the
Using tScriptRule component in Talend DI 4.2.1. Using the jexl expression to evaluate the
Using Trigger.io's barcode api in an application that only contains boilerplate HTML and the
Using C++ preprocessor directives, is it possible to test if a preprocessor symbol has
Using php's DOMDocument->LoadHTMLFile('test.html'); keeps on returning an error to me, reporting for an error
Using IOS, I am trying to integrate the LinkedIn into the application. The integration
Using Play 2.1-RC1 I can't write simple test. Here's the action code: def echoTestTagFromXml
using ::bb::cascades::Application; #include <bb/cascades/Application> What do these two declaration mean? And are there any
I am using Robotium to functionally test an Android app. I'd like to test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.