如何在junit中输入multipart文件

时间:2014-03-07 08:40:34

标签: junit

我正在使用junit来测试Daoimpl。在这种情况下,我需要提供输入,这是多部分文件。我在网上搜索,我发现使用了mockmultipartfile,但我没有得到任何例子请给我任何一个例子,以便我使用它。 我有趣的是春天和junit4.11

2 个答案:

答案 0 :(得分:5)

我已使用[MockMultipartFile]将多部分文件作为junit的输入。

FileInputStream inputFile = new FileInputStream( "path of the file");  
MockMultipartFile file = new MockMultipartFile("file", "NameOfTheFile", "multipart/form-data", inputFile); 

现在将文件输入用作多部分文件。

答案 1 :(得分:0)

在使用 Mockito 进行单元测试的情况下,因此仅用于模拟参数,我添加如下不需要 if 文件只是为了模拟

MockMultipartFile kmlfile = new MockMultipartFile("data", "filename.kml", "text/plain", "some kml".getBytes());

以上代码有助于创建仅用于模拟的模拟文件

以下是模拟的控制器服务

@RestController
public class LayerDataController {

    @PostMapping(value = "/saveOnflyMaplayerdata", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Map<String, Object> saveOnflyMaplayerdata(
            @RequestParam("name") String name, 
            @RequestParam("fileData") MultipartFile fileData) {
        try {
            boolean isValidFile = false;
            if (!fileData.isEmpty()) {

                String fileName = fileData.getOriginalFilename();
                LOGGER.info("fileName : " + fileName);
                String filetype = FilenameUtils.getExtension(fileName);
                LOGGER.info("fileName : " + filetype);

                switch (filetype) {

                case "csv":
                    isValidFile = true;
                    break;

                case "kml":
                    isValidFile = true;
                    break;

                case "kmz":
                    isValidFile = true;
                    break;

                default:
                    isValidFile = false;
                    break;
                }

                if (isValidFile) {
                    //call save method
                } else {
                    Map<String, Object> response = new HashMap<>();
                    response.put("status", false);
                    response.put("err_msg", "Unexpected Filetype/extection failed uploading");
                    return response;
                }
            }
        } catch (Exception e) {
            LOGGER.error(e);
        }
        return null;
    }

}

使用 Mockito 编写的测试用例(模拟多部分文件)

import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import com.example.service.OnflyFileSave;

@RunWith(MockitoJUnitRunner.class)
public class LayerDataControllerTest  {

    @Mock
    OnflyFileSave onfly;
    
    @InjectMocks
    LayerDataController testclass;
    
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

    }

    @Test
    public void testSaveOnflyMaplayerdataCsv() {
    //no file is path is used as simpley mocking is done just to Mock in method level
        MockMultipartFile csvfile = new MockMultipartFile("data", "filename.csv", "text/plain", "some csv".getBytes());
        testclass.saveOnflyMaplayerdata("name", csvfile);
    }

    @Test
    public void testSaveOnflyMaplayerdataKml() {
        MockMultipartFile kmlfile = new MockMultipartFile("data", "filename.kml", "text/plain", "some kml".getBytes());
        testclass.saveOnflyMaplayerdata("name", kmlfile);
    }
}  

spring boot 中使用的包如下

<!-- For Mockito Spring boot -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test</artifactId>
    <scope>test</scope>
</dependency> 
<!-- For Mockito Spring boot -->

<!-- For Mock multipart -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
</dependency>
<!-- For Mock multipart -->