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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:31:17+00:00 2026-05-28T01:31:17+00:00

I am using spring 3.0 org.springframework.web.multipart.commons.CommonsMultipartFile for file uploading. i want write down unit

  • 0

I am using spring 3.0 org.springframework.web.multipart.commons.CommonsMultipartFile for file uploading. i want write down unit test case for file upload using Mockito.

Following is my controller class

private RegistrationService registrationService;

@RequestMapping(method = RequestMethod.POST)
    public String create(Registration registration, BindingResult result,ModelMap model)
            throws NumberFormatException, Exception {
        
        File uploadedFile = uploadFile(registration);
        List<Registration> userDetails = new ArrayList<Registration>();
        processUploadedFile(uploadedFile,userDetails);
        
        model.addAttribute("userDetails", userDetails);
        
        return "Registration";
    }
    
    private File uploadFile(Registration registration) {
        
        Date dt = new Date();
        SimpleDateFormat format = new SimpleDateFormat("MM_dd_yyyy_HH_mm_ss");
        File uploadedFile = new File(uploadFileLocation
                + registration.getFileData().getOriginalFilename() + "."
                + format.format(dt));
         
        try {
            registration.getFileData().transferTo(uploadedFile);
            
        } catch (IllegalStateException e) {
            logger.error("Error occurred while uploading file", e);  
        } catch (IOException e) {
            logger.error("Error occurred while uploading file", e);
        }   
        
        return uploadedFile;
    }

    private void processUploadedFile(File uploadedFile, List<Registration> userDetails)
            throws NumberFormatException, Exception {

        registrationService.processFile(uploadedFile, userDetails);
    }

Registration class

public class Registration {
    private String name;
    private CommonsMultipartFile fileData;
    
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public CommonsMultipartFile getFileData() {
        return fileData;
    }

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

My Service class method

public void processFile(File uploadedFile,
            List<Registration> userDetails) throws NumberFormatException,
            Exception {     

        String record = "";

        try {

            FileWriter fstream = new FileWriter(outputFileLocation
                    + uploadedFile.getName());
            BufferedWriter out = new BufferedWriter(fstream);

            BufferedReader br = new BufferedReader(new FileReader(
                    uploadedFile.getAbsolutePath()));

            while ((record = br.readLine()) != null) {
                String[] requesterUser = record.split(",");

                if (insertUserToDB(requesterUser, userDetails) > 0)
                    out.write(record + ", Yes");
                else
                    out.write(record + ", No");

                out.newLine();

            }
            out.flush();
            out.close();

        } catch (FileNotFoundException e) {
            logger.error("Error occured while processing file",  e);
        } catch (IOException e) {
            
            logger.error("Error occured while processing file",  e);
        }

        
    }

How to Unit Test it?I am new to Mockito.

Edit

I have write down Test method for testing above code, but it return null for multipartFile. How should I test above service class processFile method?

@Test
       public void testProcessFile() {
        private static final String TEST_FILE = "c:\\user.csv";
        MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
        MockMultipartHttpServletRequest mockMultipartHttpServletRequest = (MockMultipartHttpServletRequest)request;
        MultipartFile multipartFile = (MultipartFile) mockMultipartHttpServletRequest.getFile(TEST_FILE);

        Registration registration=new Registration();
        registration.setFileData(multipartFile);
        RegistrationService.processFile(uploadedFile, userDetails);
        }

I am new to JUnit and mockito. Any help or pointer really appreciated.

  • 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-28T01:31:17+00:00Added an answer on May 28, 2026 at 1:31 am

    You might want to consider using Spring’s Mock implementation (org.springframework.mock.web.MockMultipartFile) rather than creating a mock using Mockito.

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

Sidebar

Related Questions

I am developing a web application using Spring and Tomcat 7.0. When I test
I'm trying to write a web application using Spring MVC. I have a configuration
I am running NTLM using Spring Security, I am getting the following error org.springframework.beans.factory.NoSuchBeanDefinitionException:
In a web application written using spring-MVC, I want to allow users to change
I am using htmlparser (htmlparser.org) to re-write all the link's in a input String.
I am using Spring Forms for my web application. For nested properties, the form
I'm trying to create a multiaction web controller using Spring annotations. This controller will
i'm creating a very simple (hello World quality) web application using spring mvc 3.0.
I'm using Spring 2.5 MVC and want to add another third-party Servlet. The Problem
I'm trying to make a sample SOAP web service using Spring-WS and JAXB and

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.