首页 > 解决方案 > 如何为junit测试加载ApplicationContext以测试文件上传到控制器发布方法

问题描述

我正在使用 spring boot 和 maven 编写一个微服务,它允许上传图像以提取文本,使用 tess4j 并将其发送回用户。但是,我正在为控制器编写 JUnit 测试用例,并且在加载 MockMvc 的应用程序上下文以将 MockMultipartFile 发送到处理来自用户的调用的控制器时遇到问题。

在询问之前我已经尝试研究过这个问题,我知道我需要访问应用程序上下文来运行对控制器的请求或使用 MockMvcBuilders StandaloneSetup 来访问控制器但是我无法让standaloneSetup 也能正常工作。

加载 ApplicationContext 失败的当前堆栈跟踪是这样的:

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:190)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:132)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multipartResolver' defined in com.imageProcessing.WebConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.multipart.MultipartResolver]: Factory method 'multipartResolver' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:456)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
... 25 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.multipart.MultipartResolver]: Factory method 'multipartResolver' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622)
... 43 more
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory
at com.imageProcessing.WebConfig.multipartResolver(WebConfig.java:17)
at com.imageProcessing.WebConfig$$EnhancerBySpringCGLIB$$d72f8dbc.CGLIB$multipartResolver$0(<generated>)
at com.imageProcessing.WebConfig$$EnhancerBySpringCGLIB$$d72f8dbc$$FastClassBySpringCGLIB$$ed0f4e04.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
at com.imageProcessing.WebConfig$$EnhancerBySpringCGLIB$$d72f8dbc.multipartResolver(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 44 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 55 more

目前控制器看起来像这样:

@Controller
public class ImageProcessingController {
    @Value("$spring.application.fileLocation")
    private String tempFileLocation;

    @PostMapping("/imageUpload")
    public TextResponse handleImageFileUpload(@RequestParam("file") MultipartFile file){
        TextResponse textExtracted;
        if(file.isEmpty()){
            textExtracted = new TextResponse("File is empty, Please upload file containing data");
        }else {
            String filename = file.getOriginalFilename();
            try{
                File tempFile = File.createTempFile(tempFileLocation,filename.substring(filename.lastIndexOf(".")+1));
                tempFile.deleteOnExit();
                file.transferTo(tempFile);
                textExtracted = new TextResponse(TextExtractor.getText(tempFile));;
                tempFile.delete();
            }catch(IOException e) {
                e.printStackTrace();
                textExtracted = new TextResponse("Error processing image");
            }           
        }
        return textExtracted;
    }
}

JUnit 代码如下所示:

@WebAppConfiguration
@ContextConfiguration(classes = WebConfig.class)
@RunWith(SpringRunner.class)
@SpringBootTest
public class ImageProcessingControllerTests {

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Test
    public void test() throws Exception{
        File image = ResourceUtils.getFile("classpath:image1.png");
        byte[] data = FileUtils.readFileToByteArray(image);
        MockMultipartFile imageFile = new MockMultipartFile("file", image.getName(), "image/png", data);

        MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
        mockMvc.perform(MockMvcRequestBuilders.multipart("/imageUpload").file(imageFile)).andExpect(status().isOk());

    }
}

WebConfig 类如下所示:

@Configuration
@ComponentScan({ "test.controllers" })
@EnableWebMvc
public class WebConfig extends WebMvcConfigurationSupport {
    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        return multipartResolver;
    }
}

我在网上看到了一些关于应用程序上下文 xml 文件的示例,但我一直无法看到文件中的内容。我确实有一个 appplication.properties 文件,它只设置控制器的上传位置。

我不确定如何加载应用程序上下文以使测试工作并获得对控制器的访问权限。我知道文本提取工作基于另一个仅访问 TextExtractor 类的测试。

项目的文件结构如下所示

imageProcessing
     src
         main
              java
                  com.imageProcessing
                  com.textExtraction
              resources
                  application.properties
         test
              java
                  com.imageProcessing
                  com.textExtraction
              resources
                  images

我只是想让测试在没有任何配置问题的情况下运行,这样我就可以开始测试控制器以查看它是否工作。

标签: mavenspring-bootpostjunitapplicationcontext

解决方案


推荐阅读