简体   繁体   English

使用getResourceAsStream()从src / test / resources获取文件

[英]get file from src/test/resources using getResourceAsStream()

file structure here 文件结构在这里

++add i want to validate json based on json schema file. ++添加我想基于json模式文件验证json。 I currently save json as string , and try to read json schema file(which is test.json), then validate. 我目前将json保存为string,并尝试读取json模式文件(即test.json),然后进行验证。 however, reading schema file is always null.. 但是,读取架构文件始终为null。

below code are I tried so far. 到目前为止,我尝试了以下代码。

++original post. ++原始帖子。

this is my current situaction. 这是我目前的情况。 I've tried many ways to get the file(test.json)... 我已经尝试了多种方法来获取文件(test.json)...

like 喜欢

InputStream in;



1.  in= valid.class.getResourceAsStream("/test.json"); 
2.  in = getClass().getClassLoader().getResourceAsStream("/test.json");
3.   in = Thread.currentThread().getContextClassLoader().getResourceAsStream("//test.json");
4. in=getClass().getResourceAsStream("/test.json"); 

5. in=ValidationUtils.class.getResourceAsStream("../resources/ds/test.json"); 

6. ClassLoader classLoader =  getClass().getClassLoader();
   in = classLoader.getResourceAsStream("test.json");
7.in=getClass().getResourceAsStream("/ds/test.json"); //tried just now


System.out.println("in"+in); //this is always null.

nothing.. work.. above.. please help me... 没有..工作..以上..请帮助我...

You can't (by default) access src/test/resources resources from code within src/main/java . 您(默认)不能从src/main/java代码访问src/test/resources资源。 All src/test/resources resources are only available for tests usually stored in src/test/java and executed during test phase (in Maven). 所有src/test/resources资源仅适用于通常存储在src/test/java并在test阶段(在Maven中)执行的test During that phase, test code can access that resources by some (not all) of the methods you quoted in your question. 在该阶段, 测试代码可以通过您在问题中引用的某些(不是全部)方法来访问该资源。

For your main classes, the resources root is src/main/resources . 对于您的主类,资源根目录为src/main/resources You should store your JSON schema file there. 您应该在其中存储JSON模式文件。 Test code can also access resources stored there (so no duplication needed), but not vice versa. 测试代码也可以访问存储在其中的资源(因此不需要重复),反之亦然。

When getting resources, the resources folder is the root. 获取资源时,资源文件夹是根目录。 If the file is in a subfolder, you need to include it in your path. 如果文件在子文件夹中,则需要将其包括在路径中。 Use 采用

in = getClass().getResourceAsStream("/ds/test.json");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从src / test / resources进行单元测试中的NIO加载文件 - NIO load file in an unit test from src/test/resources 在 JUnit 测试中使用来自 src/main/resources 的实际属性文件和 Spring @PropertySource - Using actual properties file from src/main/resources with Spring @PropertySource in JUnit test get getResourceAsStream的单元测试 - Unit Test for get getResourceAsStream 如何在JUnit测试中从src / main / resources中读取文件? - How to read file from src/main/resources relative in a JUnit test? 如何从Java中的src / test / resources中读取Excel文件 - How to read a excel file from src/test/resources in Java 使用 PathMatchingResourcePatternResolver 获取 WAR 文件中的 src/main/resources 时遇到问题 - Trouble using PathMatchingResourcePatternResolver to get src/main/resources in WAR file 更改位于src / test / resources中的属性文件 - Change property file located in src/test/resources 在路径“” src / test / resources /”上找不到文件 - file not found on path “”src/test/resources/" 从测试运行时,getResourceAsStream(“file”)在哪里搜索? - Where is getResourceAsStream(“file”) searching when running from a test? 如何从src / main / java中的src / test / resources中读取道具 - How to read props from src/test/resources in src/main/java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM