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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:37:31+00:00 2026-06-14T18:37:31+00:00

I have method for which I need to create a JUnit test: public class

  • 0

I have method for which I need to create a JUnit test:

public class MyClass {
  private String file1;
  private String file2;

  public void myMethodSpaceCheck(){
    if (new File(file1).size() > new File(file2).size() {
       throw new Exception .....  
    } 
  }
}

Is it possible to use Mockito to create that JUnit test?

  • 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-14T18:37:32+00:00Added an answer on June 14, 2026 at 6:37 pm

    When dealing with files in Java, my preferred option is to go with Apache VFS, as I can then treat them as any other POJO. Obviously, that’s a lot of work when you are already stuck with the File API.

    Another option is to forget Mockito entirely and write those files on the system. I usually avoid that, as it sometimes make it harder to have tests run in parallel on some systems.

    For this specific situation, my solution is generally to provide a special class, say FileBuilder, that can instantiate new Files:

    public class FileBuilder {
      public java.io.File newFile(String pathname) {
        return new java.io.File(pathname);
      }
    }
    

    I then mock this class before passing it to MyClass, and instrument it as appropriate:

    @Test(expected = Exception.class)
    public void should_fail_when_file1_is_bigger_than_file2() {
      FileBuilder mockFile1 = file(2L);
      FileBuilder mockFile2 = file(1L);
      FileBuilder mockFileBuilder = mock(FileBuilder.class);
      when(mockFileBuilder.newFile("file1").thenReturn(mockFile1);
      when(mockFileBuilder.newFile("file2").thenReturn(mockFile2);
    
      new MyClass(mockFileBuilder).myMethodSpaceCheck();
    }
    
    private static File file(long length) {
        File mockFile = mock(File.class);
        when(mockFile.length()).thenReturn(length);
        return mockFile;
    }
    

    (your example mentions File.size(); I assumed you meant File.length())

    The actual implementation of MyClass would look like this:

    public class MyClass {
      private String file1;
      private String file2;
      private final FileBuilder fileBuilder;
    
      public MyClass() {
        this(new FileBuilder());
      }
    
      @VisibleForTesting
      MyClass(FileBuilder fileBuilder) {
        this.fileBuilder = fileBuilder;
      }
    
      public void myMethodSpaceCheck() //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method which returns single word as a String. I need to
i have a string and i need a utility method which looks for bad
I have a method I need to unit test, which as the subject implies,
Hi I have a helper method, which returns a string, where i need to
Let's imagine I have a Service method which creates a customer: public Customer CreateCustomer(string
I have a Game class that has a -(void) play method which will be
I have a method myButtonAction which performs some heavy calculations, which I need to
I have a method which i want to convert to Extension Method public static
I need to create module in Magento which will have few database tables. One
I've got the following problem; I need to create a method, which generates a

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.