我正在用JUnit测试道普。在其中一种情况下,我需要提供一个输入,它是一个多部分文件。我在互联网上搜索,发现使用了mockmultipartfile,但没有找到任何例子。请给我一个更好的理解。
我使用的是spring和JUnit 4.11
发布于 2014-03-11 09:52:11
我使用[MockMultipartFile]将多部分文件作为junit的输入。
FileInputStream inputFile = new FileInputStream( "path of the file");
MockMultipartFile file = new MockMultipartFile("file", "NameOfTheFile", "multipart/form-data", inputFile); 现在使用文件输入作为多部分文件。
发布于 2021-01-04 13:40:11
在使用Mockito进行单元测试的情况下,因此仅用于模拟参数,我添加的参数如下,如果文件仅用于模拟
MockMultipartFile kmlfile = new MockMultipartFile("data", "filename.kml", "text/plain", "some kml".getBytes());上面的代码帮助创建了仅用于模拟的模拟文件。
下面是正在模拟的Controller服务
@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引导中使用的包
<!-- 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 -->https://stackoverflow.com/questions/22245261
复制相似问题