Announcement Announcement Module
Collapse
No announcement yet.
Multipart Fileupload-Problem in fileUploadAction Junit testing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multipart Fileupload-Problem in fileUploadAction Junit testing

    Hello,

    I have implemented fileupload functionality using MultipartFile, RequestContext and code is working as expected.I am able to upload the file from the UI and able to process it as expected. However, I am facing problem in writing the Junit test for the method doExecute(RequestContext context).

    I am using MockRequestContext and MockMultipartFile and MockParameterMap. The problem is when I pass the MockRequestContext object, I was not able to pass the MockMultipartFile object with MockParameterMap(As this map supports <String key and String value>). So, the Parameter map is empty.

    Please suuggest me a way such that getRequiredMultipartFile("file") should get the file content.

    Here is the code sample for your reference.

    Thanks in advance.


    public class FileUploadAction extends AbstractAction {

    protected final Logger logger = Logger.getLogger(getClass());


    protected Event doExecute(RequestContext context) throws Exception {

    MultipartFile file = context.getRequestParameters().getRequiredMultipar tFile("file");

    if (file.getSize() > 0) {
    logger.trace("file " + file.getOriginalFilename() );
    String content = new String(file.getBytes()).trim();
    return success();
    } else {
    return error();
    }
    }

    }

    Here is the Junit Test case.


    public void testDoExecute() throws Exception {

    FileUploadAction bean = (FileUploadAction)ac.getBean("fileUpload");

    FileInputStream inputFile = new FileInputStream("test/control/action/test1.csv");
    MockMultipartFile file = new MockMultipartFile("file", "test1.csv", "multipart/form-data", inputFile);


    MockParameterMap mpmap = new MockParameterMap();
    //By default, MockParameterMap can store string key and string values.So, I commented out below line.
    // mpmap.put("filekey","fileval")


    MockRequestContext context = new MockRequestContext(mpmap);

    Event e = bean.doExecute(context);
    assertEquals("success",e.getId())

    }

    I am getting IllegalArgument Exception at line:
    MultipartFile file = context.getRequestParameters().getRequiredMultipar tFile("file");

  • #2
    I fixed the above issue by making use of HashMap and LocalParameterMap to store the MockMultipartFile Object and passed the LocalParameterMap to MockRequestContext.

    Now I am able to run the Junit test case as expected.

    Here is the updated Junit Test case.

    public void testDoExecuteFileUpload() throws Exception {

    FileUploadAction bean = (FileUploadAction)ac.getBean("FileUpload");
    FileInputStream inputFile = new FileInputStream("test/test.csv");
    MockMultipartFile file = new MockMultipartFile("file", "test.csv", "multipart/form-data", inputFile);

    Map<String, MockMultipartFile> hmap = new HashMap<String, MockMultipartFile>();

    hmap.put("file", file);
    LocalParameterMap lmap = new LocalParameterMap(hmap);

    MockRequestContext context = new MockRequestContext(lmap);

    Event e = bean.doExecute(context);
    assertEquals("success",e.getId())

    }

    Comment


    • #3
      This is helpful. I was trying to pass collection and was wondering how to do it.
      In my case I have to pass collection of values as well as MockRequestContext with name and value parameter.
      Have you ever tried that ?

      Comment

      Working...
      X