javaguy
javaguy

Reputation: 1133

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?

Upvotes: 1

Views: 3238

Answers (2)

javaguy
javaguy

Reputation: 1133

Got resolution as below :

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

Upvotes: 2

Sambit
Sambit

Reputation: 8031

Since you are mocking, you should use like this.

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

The actual structure should be like this.

MockMultipartFile mockMultipartFile = new MockMultipartFile("test", "test.xml", <XML MEDIA TYPE>,
      "String contents".getBytes()));

For more reference, see below the links. https://www.codota.com/code/java/classes/org.springframework.mock.web.MockMultipartFile

Using Spring MVC Test to unit test multipart POST request

Upvotes: 0

Related Questions