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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:55:21+00:00 2026-06-12T21:55:21+00:00

I would like to mock a File and FileInputStream Object for an JUnit test.

  • 0

I would like to mock a File and FileInputStream Object for an JUnit test.

Let us say we are having a Parser. From outside the Parser is just accessible via the parse(File myFile) method. The other methods like readMyStream….are private.

Example Code:

public class Parser {

   public HashMap<String, String> parse(File myFile) throws Exception {
         HashMap<String, String> myConfig;
         Config config;

         try {
             //this line gives me a headache
             FileInputStream myFileInputStream = new FileInputStream(myFile);

             configStream = readMyStream(myFileInputStream);
               ..........
         } catch (FileNotFoundException e) {
                throw e;
         }
         return myConfig;
   }
   //reads the stream 
   private Config readMyStream(FileInputStream myFileInputStream) {
      Config config;    

      ...JDOM stuff....    

      return config;
   }
}

The problems I face:

  • how to assert a File Object an FileInputStream (PowerMockito) like this File belongs to this FileInputStream with the following content
  • how to mock a private method (Mockito/PowerMockito)

an example of the Mocking/not working :)….

public class ParserTest {

   @Test
   public final void testParse() {
      File MOCKEDFILE = PowerMockito.mock(File.class);
      PowerMockito.when(MOCKEDFILE.exists()).thenReturn(true);
      PowerMockito.when(MOCKEDFILE.isFile()).thenReturn(true);
      PowerMockito.when(MOCKEDFILE.isDirectory()).thenReturn(false);
      PowerMockito.when(MOCKEDFILE.createNewFile()).thenReturn(true);
      PowerMockito.when(MOCKEDFILE.length()).thenReturn(11111111L);

      //what about the path of MOCKEDFILE which isn't existing
      PowerMockito.when(MOCKEDFILE.getPath()).thenReturn(?????);

      //how to assign a File an FileInputStream? (I thought something like)
      PowerMockito.mockStatic(FileInputStream.class);
      FileInputStream MOCKEDINPUTSTREAM = PowerMockito.mock(FileInputStream.class);
      PowerMockito.whenNew(FileInputStream.class).withArguments(File.class).thenReturn(MOCKEDINPUTSTREAM);

      //how to mock the private method readMyStream
   }
  • 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-12T21:55:22+00:00Added an answer on June 12, 2026 at 9:55 pm

    Rather than mocking File, I would suggest using the TemporaryFolder Rule and create / not create the file as appropriate.

    In my previous projects, we have written a FileInputStreamSupplier class that is used to create a FileInputStream. This class can then be mocked to provide a mocked FileInputStream to allow for testing behavior. Generically, you could have your class take a Supplier<FileInputStream> (using Guava) and mock that.

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

Sidebar

Related Questions

Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
We would like to use Selenium to test email content generated from our website.
I would like to unit test the Initialize method of a Controller object. The
Possible Duplicate: .NET file system wrapper library I would like to write a test
I'm using python-mock to mock out a file open call. I would like to
I need to test a method that would move a file. I don't want
I would like to be able to mock the ClientScriptManager class on a webforms
I would like an EasyMock mock to be able to expect an empty list
I am trying to do some javascript test driven development, and would like to
I would like to Mock the System.IO.FileInfo.Extension Method and have it return .xls but

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.