编程语言您现在的位置是:首页 > 博客日志 > 编程语言

Test Failed to load ApplicationContext Caused by: java.io.FileNotFoundException: class path resource

<a href='mailto:'>微wx笑</a>的头像微wx笑 2020-05-26编程语言240 4 0关键字:   

如果你遇到了 Test Failed to load ApplicationContext Caused by: java.lang.IllegalArgumentException: Could not reso 这个问题,既然*.xml的Mapping文件没有打包进来,你是不是就应该想到.properties估计也没有打包进来?由于“spring-mybatis.xml”文件是在“src/main/resources”目录下,所以最终错误是: Caused by: java.io.FileNotFou

其实遇到这个问题我首先注意到的是几条警告信息:yyD无知

1
2
3
log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

完整的错误信息如下:
yyD无知

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building yuyi Maven Webapp 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ yuyi ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 45 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ yuyi ---
[INFO] Compiling 233 source files to /Users/aven/Documents/workspace/yuyi/target/classes
[WARNING] /Users/aven/Documents/workspace/yuyi/src/main/java/com/wanyu/frame/util/ImageUtils.java:[17,15] BASE64Decoder是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/aven/Documents/workspace/yuyi/src/main/java/com/wanyu/frame/util/ImageUtils.java:[18,15] BASE64Encoder是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/aven/Documents/workspace/yuyi/src/main/java/com/wanyu/frame/util/StringUtil.java:[17,15] BASE64Decoder是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/aven/Documents/workspace/yuyi/src/main/java/com/wanyu/frame/util/StringUtil.java:[18,15] BASE64Encoder是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/aven/Documents/workspace/yuyi/src/main/java/com/wanyu/frame/util/ImageUtils.java:[36,2] BASE64Decoder是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/aven/Documents/workspace/yuyi/src/main/java/com/wanyu/frame/util/ImageUtils.java:[36,30] BASE64Decoder是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/aven/Documents/workspace/yuyi/src/main/java/com/wanyu/frame/util/ImageUtils.java:[83,2] BASE64Encoder是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/aven/Documents/workspace/yuyi/src/main/java/com/wanyu/frame/util/ImageUtils.java:[83,30] BASE64Encoder是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/aven/Documents/workspace/yuyi/src/main/java/com/wanyu/frame/util/StringUtil.java:[199,21] BASE64Encoder是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/aven/Documents/workspace/yuyi/src/main/java/com/wanyu/frame/util/StringUtil.java:[209,14] BASE64Encoder是内部专用 API, 可能会在未来发行版中删除
[WARNING] /Users/aven/Documents/workspace/yuyi/src/main/java/com/wanyu/frame/util/StringUtil.java:[221,25] BASE64Decoder是内部专用 API, 可能会在未来发行版中删除
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ yuyi ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/aven/Documents/workspace/yuyi/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ yuyi ---
[INFO] Compiling 3 source files to /Users/aven/Documents/workspace/yuyi/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ yuyi ---
[INFO] Surefire report directory: /Users/aven/Documents/workspace/yuyi/target/surefire-reports
 
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.wanyu.yuyi.test.TestMyBatis
log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.475 sec <<< FAILURE!
test1(com.wanyu.yuyi.test.TestMyBatis)  Time elapsed: 0.13 sec  <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:319)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:212)
    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:232)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:175)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    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.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [spring-mybatis.xml]; nested exception is java.io.FileNotFoundException: class path resource [spring-mybatis.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:235)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:118)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:250)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
    ... 31 more
Caused by: java.io.FileNotFoundException: class path resource [spring-mybatis.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:158)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
    ... 43 more
 
 
Results :
 
Tests in error: 
  test1(com.wanyu.yuyi.test.TestMyBatis): Failed to load ApplicationContext
 
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.148 s
[INFO] Finished at: 2020-05-26T10:51:00+08:00
[INFO] Final Memory: 31M/284M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project yuyi: There are test failures.
[ERROR] 
[ERROR] Please refer to /Users/aven/Documents/workspace/yuyi/target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

如果你遇到了 Test Failed to load ApplicationContext Caused by: java.lang.IllegalArgumentException: Could not reso 这个问题,既然*.xml的Mapping文件没有打包进来,你是不是就应该想到.properties估计也没有打包进来?yyD无知

image.pngyyD无知

由于“spring-mybatis.xml”文件是在“src/main/resources”目录下,所以最终错误是: Caused by: java.io.FileNotFoundException: class path resource [spring-mybatis.xml] cannot be opened because it does not existyyD无知

所以解决的方法是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <!-- 是否替换资源中的属性 -->
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <!-- <includes> <include>**/*.properties</include> <include>**/*.xml</include> 
            </includes> <filtering>true</filtering> -->
    </resource>
</resources>

把.properties文件也包含进来。yyD无知


yyD无知

本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0 许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/blog/lang/2020-05-26/454.html

很赞哦! (10) 有话说 (0)

相关文章

文章评论