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

  • Home
  • SEARCH
  • 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 6183063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:18:13+00:00 2026-05-24T01:18:13+00:00

I have a junit test method that takes a CommonsMultipartFile object as a parameter.

  • 0

I have a junit test method that takes a CommonsMultipartFile object as a parameter.

I’m trying to create a FileItem object so I can pass it to the constructor,

CommonsMultipartFile(org.apache.commons.fileupload.FileItem fileItem)

To do that, I’m trying to create the FileItem object using the DiskFileItem constructor,

DiskFileItem(java.lang.String fieldName, java.lang.String contentType, boolean isFormField, java.lang.String fileName, int sizeThreshold, java.io.File repository)

but I’m not sure how to pass any of those parameters.

I have all of this working in a Spring 3 MVC controller, but to do my junit tests, I need to pass a method two objects. One is the UploadItem object which looks like the following,

import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class UploadItem {
 private String fileName;
 private String filePath;
 private CommonsMultipartFile fileData;

 public String getFileName() {
  return fileName;
 }

 public void setFileName(String fileName) {
  this.fileName = fileName;
 }

 public String getFilePath() {
  return filePath;
 }

 public void setFilePath(String filePath) {
  this.filePath = filePath;
 }

 public CommonsMultipartFile getFileData() {
  return fileData;
 }

 public void setFileData(CommonsMultipartFile fileData) {
  this.fileData = fileData;
 }
}

The setFileData() method requires the CommonsMultipartFile object which I’m trying to create just given a file in my src/test/resources directory.

Would anyone know how I can take a file, create a FileItem object and pass that to the CommonsMultipartFile object constructor?

Thanks. If anything is unclear, please let me know – I’m not that familiar with Spring MVC file uploads.

  • 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-24T01:18:15+00:00Added an answer on May 24, 2026 at 1:18 am

    Use the more common interface org.springframework.web.multipart.MultipartFile. instead of
    org.springframework.web.multipart.commons.CommonsMultipartFile in your Command (UploadItem). (CommonsMultipartFile is a 1:1 implementation of the Interface).

    Now you can create an instance of CommonsMultipartFile with the mock class org.springframework.mock.web.MockMultipartFile. (which is element of spring-test.jar).

    Then the creation of an MultipartFile in the tests is only one statement, without any cast:

    MockMultipartFile mockMultipartFile = new MockMultipartFile(
           "test.txt",                //filename
           "Hallo World".getBytes()); //content
    
    • 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 method that pretty much takes anything as a
I have a JUnit test that tests adding Strings to a Dictionary custom type.
I'm trying to create an EJB factory class, which works like this: You have
Suppose, I have a junit test class: class MyComponentTest { private void test(File file)
I have a JUnit 3.x TestCase which I would like to be able to
I'm dipping my toes into Android development. I have a project that will interface
I have a class I'm unit testing that requires fairly extensive database setup before
I've been looking for a method that operates like Arrays.equals(a1, a2) , but ignoring
I have not used Junit before and have not done unit testing automatically. Scenario:
I just used MyEclipse to automatically generate some JUnit test cases. One of the

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.