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 7914073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:59:42+00:00 2026-06-03T13:59:42+00:00

I am trying to create a file in the Android JUNIT testcase setup: protected

  • 0

I am trying to create a file in the Android JUNIT testcase setup:

protected void setUp() throws Exception {
        super.setUp();

        File storageDirectory = new File("testDir");
        storageDirectory.mkdir();

        File storageFile = new File(storageDirectory.getAbsolutePath()
                + "/test.log");

        if (!storageFile.exists()) {
            storageFile.createNewFile();
        }
        mContext = new InternalStorageMockContext(storageFile);
        mContextWrapper = new ContextWrapper(mContext);
    }

I get an exception No Such File or Directory when I call createNewFile. Is there a standard way for creating files from Android JUNIT?

  • 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-03T13:59:43+00:00Added an answer on June 3, 2026 at 1:59 pm

    In Android, directory/file creation and access are usually managed by Context. for instance, this is typically how we create directory file under application’s internal storage:

    File testDir = Context.getDir("testDir", Context.MODE_PRIVATE);
    

    Check out the API, there are many other useful method getXXXDir() you can use for creating/accessing file.

    Back to JUnit topic, suppose you use ActivityInstrumentationTestCase2 and your application project has package name com.example, and your test project has package name com.example.test:

    // this will create app_tmp1  directoryunder data/data/com.example.test/,
    // in another word, use test app's internal storage.
    this.getInstrumentation().getContext().getDir("tmp1", Context.MODE_PRIVATE);
    
    // this will create app_tmp2 directory under data/data/com.example/,
    // in another word use app's internal storage.
    this.getInstrumentation().getTargetContext().getDir("tmp2", Context.MODE_PRIVATE);
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a file from Android Junit in the internal storage
I am trying to create a file using File newFile = new File(myFile); However
I am trying to create and use jar file in an Android project under
I'm all new to Android and I'm trying to create a spinner programmatically and
I'm trying to create an ePub file in android. Below is my source code.
I am trying to create a file with my android application. I need to
I am trying to create a simple android application. I am new to Android
I am trying to create a SQLite database file for iOS and Android. However,
I am a new android programmer and i am trying to create the following
I'm trying to create an ePub file in android. Below is my source code.

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.