使用maven获取测试文件资源
我试图获取一个json文件资源,以便使用它来构建java对象。当我尝试在Intellij中运行我的代码时,我没有任何问题,但是当我使用使用maven获取测试文件资源,maven,unit-testing,io,Maven,Unit Testing,Io,我试图获取一个json文件资源,以便使用它来构建java对象。当我尝试在Intellij中运行我的代码时,我没有任何问题,但是当我使用mvn test运行时,我得到以下错误 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project cadastur-backend-business: Execution default-te
mvn test
运行时,我得到以下错误
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project cadastur-backend-business: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test failed: There was an error in the forked process
[ERROR] java.lang.RuntimeException: Unable to create test class 'guiaVOTemplate.json'
[ERROR] at org.apache.maven.surefire.util.DefaultScanResult.loadClass(DefaultScanResult.java:135)
[ERROR] at org.apache.maven.surefire.util.DefaultScanResult.applyFilter(DefaultScanResult.java:95)
[ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.scanClassPath(JUnit4Provider.java:222)
[ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:107)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
[ERROR] Caused by: java.lang.ClassNotFoundException: guiaVOTemplate.json
[ERROR] at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
[ERROR] at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
[ERROR] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
[ERROR] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[ERROR] at org.apache.maven.surefire.util.DefaultScanResult.loadClass(DefaultScanResult.java:131)
[ERROR] ... 6 more
[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/PluginExecutionException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <goals> -rf :cadastur-backend-business
将
guiaVOTemplate.json
文件复制到src\main\resources
中,它应该可以工作。我发现我的错误,在mainpom.xml
中,maven surefire插件包含了所有文件,因此我无法创建测试类“guiaVOTemplate.json”。因此,我可以将扩展名为.json
的文件放入排除列表,如下所示。我的guiaVOTemplate.json
仍保留在src/test/resources
中
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.json</exclude>
</excludes>
</configuration>
</plugin>
org.apache.maven.plugins
maven surefire插件
2.18.1
**/*
**/*.json
尝试将guiaVOTemplate.json
文件复制到src\main\resources
中,看看它是否有效。谢谢@RITZXAVI,现在它对我有效,但是你知道为什么我不能将json文件放入src\test\resources
?当然。顺便问一下,它是如何工作的?我按照你的建议。哦,对不起,我不明白你的评论。。。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.json</exclude>
</excludes>
</configuration>
</plugin>