添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
SpringBoot 模拟附件上传提交,提示ClassNotFoundException: org.springframework.mock.web.MockMultipartFile
最新推荐文章于 2023-04-27 11:38:13 发布
最新推荐文章于 2023-04-27 11:38:13 发布

今天在编写模拟Hadoop 文件上传功能,提示如下错误信息=ClassNotFoundException: org.springframework.mock.web.MockMultipartFile

模拟功能说明:采用spring-test.jar 包中的MockMultipartFile 类,模拟文件上传功能,但是在执行相关业务逻辑代码,提示类找不到。后来检查maven 依赖是我发现spring-test.jar 生效模式是test(测试模式),速度移除spring-test 的模式限制。

maven 配置文件如下:

报错依赖:

<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.1.3.RELEASE</version>
    <scope>test</scope>
</dependency>

解决后的依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.1.3.RELEASE</version>
</dependency>

测试功能代码:

@ApiOperation(httpMethod = "POST", value = "新建文件")
	@RequestMapping(value = "/createFile", method = { RequestMethod.POST }, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JreportResponse createFile(
			@RequestBody @ApiParam(name = "JSON对象", value = "json格式对象", required = true) JSONObject entity) {
		FileInputStream inputStream = null;
		MultipartFile file = null;
		try {
			inputStream = new FileInputStream("C:\\data\\words.txt");
			file = new MockMultipartFile("test.txt", inputStream);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage());
		}finally{
			try {
				inputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				logger.error(e.getMessage());
		service.createFile(entity.getString("path"),file);
		return JreportResponse.ok();
org.springframework.mock.jndi.ExpectedLookupTemplate.class
org.springframework.mock.jndi.SimpleNamingContext.class
org.springframework.mock.jndi.SimpleNamingContextBuilder.class
org.springframework.mock.web.DelegatingServletInputStream.class
org.springframework.mock.web.DelegatingServletOutputStream.class
org.springframework.mock.web.HeaderValueHolder.class
org.springframework.mock.web.MockExpressionEvaluator.class
org.springframework.mock.web.MockFilterChain.class
org.springframework.mock.web.MockFilterConfig.class
org.springframework.mock.web.MockHttpServletRequest.class
org.springframework.mock.web.MockHttpServletResponse.class
org.springframework.mock.web.MockHttpSession.class
org.springframework.mock.web.MockMultipartFile.class
org.springframework.mock.web.MockMultipartHttpServletRequest.class
org.springframework.mock.web.MockPageContext.class
org.springframework.mock.web.MockRequestDispatcher.class
org.springframework.mock.web.MockServletConfig.class
org.springframework.mock.web.MockServletContext.class
org.springframework.mock.web.PassThroughFilterChain.class
org.springframework.mock.web.portlet.MockActionRequest.class
org.springframework.mock.web.portlet.MockActionResponse.class
org.springframework.mock.web.portlet.MockMultipartActionRequest.class
org.springframework.mock.web.portlet.MockPortalContext.class
org.springframework.mock.web.portlet.MockPortletConfig.class
org.springframework.mock.web.portlet.MockPortletContext.class
org.springframework.mock.web.portlet.MockPortletPreferences.class
org.springframework.mock.web.portlet.MockPortletRequest.class
org.springframework.mock.web.portlet.MockPortletRequestDispatcher.class
org.springframework.mock.web.portlet.MockPortletResponse.class
org.springframework.mock.web.portlet.MockPortletSession.class
org.springframework.mock.web.portlet.MockPortletURL.class
org.springframework.mock.web.portlet.MockRenderRequest.class
org.springframework.mock.web.portlet.MockRenderResponse.class
org.springframework.test.AbstractDependencyInjectionSpringContextTests.class
org.springframework.test.AbstractSingleSpringContextTests.class
org.springframework.test.AbstractSpringContextTests.class
org.springframework.test.AbstractTransactionalDataSourceSpringContextTests.class
org.springframework.test.AbstractTransactionalSpringContextTests.class
org.springframework.test.AssertThrows.class
org.springframework.test.ConditionalTestCase.class
org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.class
org.springframework.test.annotation.DirtiesContext.class
org.springframework.test.annotation.ExpectedException.class
org.springframework.test.annotation.IfProfileValue.class
org.springframework.test.annotation.NotTransactional.class
org.springframework.test.annotation.ProfileValueSource.class
org.springframework.test.annotation.Repeat.class
org.springframework.test.annotation.SystemProfileValueSource.class
org.springframework.test.annotation.Timed.class
org.springframework.test.jpa.AbstractAspectjJpaTests.class
org.springframework.test.jpa.AbstractJpaTests.class
org.springframework.test.jpa.OrmXmlOverridingShadowingClassLoader.class
org.springframework.test.web.AbstractModelAndViewTests.class
                                    错误的类文件: /C:/Users/Administrator/.m2/repository/org/springframework/spring-context/6.0.6/spring-context-6.0.6.jar!写spring遇到的问题,该错误可能是由于JDK版本不一致引起的,可在  文件-->项目结构-->项目-->SDK 中更改一下对应的JDK版本再点击应用即可。
                                    mock-fs 是个可配置的 mock 文件系统,允许内存和 mock 文件系统短暂支持 Node 的内置  fs module 。这个允许用户对一组 mock 文件进行测试,而不是对一群测试装置。
                            标签:mockfs
                                    File picFile = new File("filePath");
FileInputStream fileInputStream = new FileInputStream(picFile);
MultipartFile multipartFile = new MockMultipartFile(picFile.getName(), picFile.getName(),
                ContentType.APPLICATION_OCTET_STREAM.toString(), 
                                    fileMultipartFile的时候会用到MockMultipartFile
当你导入spring-test依赖的时候 会跟某些依赖冲突(暂未找到具体是哪个冲突)
解决方法 重写一个类去实现MultipartFile接口
直接用MockMultipartFile的源码
public class MultipartFileDto implements MultipartFile {
    private final String name;
    private String origi
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito2</artifactId>
                                    可以看到MultipartFile是个接口
转成MultipartFile格式则需要转成实现MultipartFile接口的实现类即可,如下选择转成用MockMultipartFile实现
首先:需要先引入依赖包
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-test</artifactId>
	<version>5.3.9</