如何加载ApplicationContext进行junit测试,以将文件上传到控制器发布方法

时间:2019-05-10 20:55:54

标签: maven spring-boot post 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

我只是想在没有任何配置问题的情况下运行测试,所以我可以开始测试控制器以查看其是否正常工作。

0 个答案:

没有答案