Search code examples
javaspringspring-bootjunitmultipart

Multipart with format other than CSV not able to find file on classpath


I have file names test.csv ,test.xml ,test.text on my classpath(src/main/resources) folder.

I am creating Mutlipart for my Junit test case as follows :

MultipartFile multipartFile = new MockMultipartFile("test.csv","test.csv","text/csv",
                new FileInputStream(new File("test.csv")));

This is working fine.

However when I am trying below code for XMl,it gives FileNotFoundException.

MultipartFile multipartFile = new MockMultipartFile("test.xml","test.xml","text/xml",
                    new FileInputStream(new File("test.xml")));

Can anyone pls let me know,what can be issue?


Solution

  • Got resolution as below :

    MockMultipartFile   mockitoMultipartFile = new MockMultipartFile("test.xml","test.xml","text/xml",
                    this.getClass().getClassLoader()
                    .getResourceAsStream("test.xml"));