我有一个弹簧启动应用程序,运行没有问题。我想编写一些单元测试并遇到以下异常:
java.lang.IllegalStateException:无法加载ApplicationContext
在 org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) 在 org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108) 在 org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117) 在 org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) 在 org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:44) 在 org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246) 在 org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97) 在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at at org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 在 org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 在org.junit.runners.ParentRunner.run(ParentRunner.java:363)at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) 在org.junit.runner.JUnitCore.run(JUnitCore.java:137)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 在 com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 引起:org.springframework.beans.factory.BeanCreationException: 创建名称为' requestMappingHandlerAdapter'的bean时出错定义 在类路径资源中 [组织/ springframework的的/ boot /自动配置/网络/的servlet / WebMvcAutoConfiguration $ EnableWebMvcConfiguration.class]: 通过工厂方法进行Bean实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例 [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: 工厂方法' requestMappingHandlerAdapter'抛出异常;嵌套 异常是java.lang.NoClassDefFoundError: net / minidev / json / writer / JsonReaderI at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1250) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:541) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501) 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:317) 在 org.springframework.beans.factory.support.AbstractBeanFactory $$ LAMBDA $ 123 / 1758624236.getObject(未知 来源)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) 在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:395) 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:327) 在 org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:138) 在 org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99) 在 org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117) ... 25更多引起: org.springframework.beans.BeanInstantiationException:失败 实例 [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]: 工厂方法' requestMappingHandlerAdapter'抛出异常;嵌套 异常是java.lang.NoClassDefFoundError: net / minidev / json / writer / JsonReaderI at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) 在 org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579) ... 44更多引起:java.lang.NoClassDefFoundError: net / minidev / json / writer / JsonReaderI at com.jayway.jsonpath.internal.DefaultsImpl。(DefaultsImpl.java:17) 在 com.jayway.jsonpath.internal.DefaultsImpl。(DefaultsImpl.java:15) 在 com.jayway.jsonpath.Configuration.getEffectiveDefaults(Configuration.java:48) 在 com.jayway.jsonpath.Configuration.access $ 000(Configuration.java:34) 在 com.jayway.jsonpath.Configuration $ ConfigurationBuilder.build(Configuration.java:229) 在 org.springframework.data.web.JsonProjectingMethodInterceptorFactory。(JsonProjectingMethodInterceptorFactory.java:75) 在 org.springframework.data.web.ProjectingJackson2HttpMessageConverter.initProjectionFactory(ProjectingJackson2HttpMessageConverter.java:87) 在 org.springframework.data.web.ProjectingJackson2HttpMessageConverter。(ProjectingJackson2HttpMessageConverter.java:72) 在 org.springframework.data.web.config.SpringDataWebConfiguration.extendMessageConverters(SpringDataWebConfiguration.java:162) 在 org.springframework.web.servlet.config.annotation.WebMvcConfigurerComposite.extendMessageConverters(WebMvcConfigurerComposite.java:144) 在 org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration.extendMessageConverters(DelegatingWebMvcConfiguration.java:123) 在 org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.getMessageConverters(WebMvcConfigurationSupport.java:758) 在 org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.requestMappingHandlerAdapter(WebMvcConfigurationSupport.java:565) 在 org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration $ EnableWebMvcConfiguration.requestMappingHandlerAdapter(WebMvcAutoConfiguration.java:467) 在 org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration $ EnableWebMvcConfiguration $$ EnhancerBySpringCGLIB $$ c37b1a28.CGLIB $ requestMappingHandlerAdapter $ 8() 在 org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration $ EnableWebMvcConfiguration $$ EnhancerBySpringCGLIB $$ c37b1a28 $$ FastClassBySpringCGLIB $$ 2c18252f.invoke() 在 org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) 在 org.springframework.context.annotation.ConfigurationClassEnhancer $ BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) 在 org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration $ EnableWebMvcConfiguration $$ EnhancerBySpringCGLIB $$ c37b1a28.requestMappingHandlerAdapter() at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:497)at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ... 45更多引起:java.lang.ClassNotFoundException: net.minidev.json.writer.JsonReaderI at java.net.URLClassLoader.findClass(URLClassLoader.java:381)at java.lang.ClassLoader.loadClass(ClassLoader.java:424)at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331)at at java.lang.ClassLoader.loadClass(ClassLoader.java:357)... 69更多
我想要的只是测试@Service注释bean并正确连接它。
@RunWith(SpringRunner.class)
@SpringBootTest
public class ImageResizeTest {
@Autowired
private ImageService imageService;
@Test
public void imageResizeTest() throws IOException {
BufferedImage image = ImageIO.read(new File("E:\\tmp\\original.jpg"));
imageService.storeComicCover(image, "mytest.jpg");
}
}
@Service
public class ImageService {
private static final Logger LOG = LoggerFactory.getLogger(ImageService.class);
public static final String FORMAT_NAME = "jpg";
@Value("${image.norm.resize.height}")
private int normalResizeHeight;
@Value("${image.norm.resize.width}")
private int normalResizeWidth;
@Value("${image.thumb.resize.height}")
private int thumbResizeHeight;
@Value("${image.thumb.resize.width}")
private int thumbResizeWidth;
@Value("${image.mini.resize.height}")
private int miniResizeHeight;
@Value("${image.mini.resize.width}")
private int miniResizeWidth;
@Value("${image.comics.original.path}")
private String originalComicPath;
@Value("${image.stories.original.path}")
private String originalStoriesPath;
@Value("${image.characters.original.path}")
private String originalCharactersPath;
@Value("${image.comic.resized.path}")
private String resizeComicPath;
@Value("${image.stories.resized.path}")
private String resizeStoriesPath;
@Value("${image.characters.resized.path}")
private String resizeCharactersPath;
public ImageAttributes storeComicCover(BufferedImage originalImage, String originalImageName) {
我尝试在maven中添加缺少的lib和测试范围,但这没有效果。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.imgscalr</groupId>
<artifactId>imgscalr-lib</artifactId>
<version>4.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
我做错了什么?我感谢任何帮助。
答案 0 :(得分:0)
似乎缺少一些库。请将以下内容添加到您的pom.xml中并检查:
com.fasterxml.jackson.core jackson-databind 2.4.5
com.google.code.gson gson 2.3.1
net.minidev json-smart 2.1.1
org.slf4j slf4j-api 1.7.10
net.minidev asm 2.1.1
如果没有,问题出在其他地方。
答案 1 :(得分:0)
@SpringBootTest(classes = {... classesRequired ...})您可以尝试加载所需的类,看看它是否以这种方式工作。 SpringBootTest应该加载所需的应用程序上下文。