我试图获取一个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-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
该项目具有以下结构:
上面有代码,我试图读取文件并将json转换为java对象。
package br.gov.mtur.cadastur.builder;
import br.gov.mtur.cadastur.vo.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class GuiaVOBuilder {
private GuiaVO guia;
public GuiaVOBuilder() {
this.guia = new GuiaVO();
}
public GuiaVOBuilder comId(Integer id) {
guia.setId(id);
return this;
}
public GuiaVOBuilder comPessoaFisica(PessoaFisicaVO pessoaFisica) {
guia.setPessoaFisica(pessoaFisica);
return this;
}
... //Builder methods
public GuiaVO build(){
return guia;
}
public GuiaVO buildGuiaCompleto() throws IOException {
return new ObjectMapper().readValue(GuiaVOBuilder.class.getResource("/guiaVOTemplate.json"), GuiaVO.class);
}
}
答案 0 :(得分:0)
将guiaVOTemplate.json
文件复制到src\main\resources
,它应该有效。
答案 1 :(得分:0)
我发现我的错误,在主pom.xml
中,maven-surefire-plugin包含了所有文件,因此我得到了Unable to create test class '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>