我在junit中使用以下代码从test / resources文件夹中获取文件
new ClassPathResource(xmlFileName).getFile();
但是我看到以下错误 -
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: C:\Users\V12839\Workspaces\C-Test\c-service\bin\mock\CResponse\CResponse_1.json; line: 1, column: 1]
java.io.FileNotFoundException: class path resource [mock/CResponse/CResponse_1.json] cannot be resolved to URL because it does not exist
at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:187)
at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:48)
我使用弹簧测试套件运行测试。错误\ bin \ mock中显示的路径,但我的测试位于\ src \ test \ resources
下答案 0 :(得分:1)
你可以像这样使用classLoader:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("somefile").getFile());
System.out.println(file.getAbsolutePath());
问候。