首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在junit中提供多部分文件输入

如何在junit中提供多部分文件输入
EN

Stack Overflow用户
提问于 2014-03-07 08:40:33
回答 2查看 18.3K关注 0票数 7

我正在用JUnit测试道普。在其中一种情况下,我需要提供一个输入,它是一个多部分文件。我在互联网上搜索,发现使用了mockmultipartfile,但没有找到任何例子。请给我一个更好的理解。

我使用的是springJUnit 4.11

EN

回答 2

Stack Overflow用户

发布于 2014-03-11 09:52:11

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

代码语言:javascript
复制
FileInputStream inputFile = new FileInputStream( "path of the file");  
MockMultipartFile file = new MockMultipartFile("file", "NameOfTheFile", "multipart/form-data", inputFile); 

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

票数 10
EN

Stack Overflow用户

发布于 2021-01-04 13:40:11

在使用Mockito进行单元测试的情况下,因此仅用于模拟参数,我添加的参数如下,如果文件仅用于模拟

代码语言:javascript
复制
MockMultipartFile kmlfile = new MockMultipartFile("data", "filename.kml", "text/plain", "some kml".getBytes());

上面的代码帮助创建了仅用于模拟的模拟文件。

下面是正在模拟的Controller服务

代码语言:javascript
复制
@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编写的测试用例(模拟多部分文件)

代码语言:javascript
复制
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引导中使用的包

代码语言:javascript
复制
<!-- 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 -->
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22245261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档