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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:20:56+00:00 2026-05-30T19:20:56+00:00

I simply want to know if there is a way to mock an array

  • 0

I simply want to know if there is a way to mock an array of FTPFile.
I am trying to pass as a parameter a fake FTPFile[] to the function I want to test:

protected void indexFolder(FTPClient, FTPFile[], File, FTPFolderAssetSource);

I am using FakeFtpServer to fake, as the name says, my ftp server. This library permit to fake the ftp content in this way:

        fileSystem = new WindowsFakeFileSystem();
    DirectoryEntry directoryEntry1 = new DirectoryEntry("c:\\");
    directoryEntry1.setPermissions(new Permissions("rwxrwx---"));
    directoryEntry1.setOwner(USER1);

    FileEntry fileEntry1 = new FileEntry("c:\\data\\file1.txt", CONTENTS);
    fileEntry1.setPermissionsFromString("rw-rw-rw-");
    fileEntry1.setOwner(USER1);
    fileEntry1.setGroup(GROUP); 

    fileSystem.add(directoryEntry1);
    fileSystem.add(fileEntry1);

    ftp = new FakeFtpServer();
    ftp.setFileSystem(fileSystem);

Now, how can I use fileSystem to test my function who require FTPFile[] as parameter?

  • 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-05-30T19:20:58+00:00Added an answer on May 30, 2026 at 7:20 pm

    There is nothing specialy about the FTPFile class which would prevent mocking. Unfortunately, using Mockito, you can not mock arrays, as they are final.

    This sample code should demonstrate the problem:

    import static org.mockito.Mockito.*;
    
    import org.junit.Test;
    
    public class TestMockArrays {
      interface Animal {
        String getName();
      }
    
      @Test
      public void testMockArray() {
        final Animal[] mockArray = mock(Animal[].class);
    
        when(mockArray[0].getName()).thenReturn("cat");
        when(mockArray[1].getName()).thenReturn("dog");
        when(mockArray[2].getName()).thenReturn("fish");
    
        print1st3(mockArray);
      }
    
      public static void print1st3(final Animal[] animals) {
        System.out.println(animals[0].getName() + " " + animals[1].getName() + " " + animals[2].getName());
      }
    }
    

    Run it, and you’ll see it results in an error message which makes explicit the problem:

    org.mockito.exceptions.base.MockitoException:
    Cannot mock/spy class [LTestMockArrays$Animal;

    Mockito cannot mock/spy following:
    – final classes
    – anonymous classes
    – primitive types

    The easiest solution is to use an extension to Mockito like Powermock which gets around certain restrictions to Mockito’s mocking ability by using bytecode manipulation. Then you could create the mock array by adding the following annotations to your junit test class:

    @RunWith(PowerMockRunner.class) // Need to run with Powermock runner
    @PrepareForTest(FTPFile[].class) // We prepare FTPFile[] class for test because it's final 
    

    then in your test method you would create Mockito mock as normal:

    FTPFile[] mockFTPFiles = mock(FTPFile[].class);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know if there is a way to enable auto refresh of
I simply want to fill-up cells in my spreadsheet from a VBA function. By
is there any way to know through code what are all download requests I
I want to know the best way of transferring a cryptographic initialization vector (IV)
Is there any way I can have thread-safe coroutines along with boost::asio? I want
I would like to know if there's a way to move a project in
I want to know how to open and manipulate a simple image file in
i want to know which parsing method is best to use among Simple XML
I am having a simple code where i want to know when does onRestoreInstanceState
I know, I am asking very simple and stupid question but i want to

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.