简体   繁体   English

Spring boot:如何在单元测试中从类路径读取资源

[英]Spring boot: How to read resource from classpath in unit test

I'm trying to read a file from classpath like this in my unit test:我试图在我的单元测试中从类路径中读取一个文件:

@Value("classpath:state.json")
Resource stateFile;

I have state.json file in src/test/resources directory.我在src/test/resources目录中有state.json文件。 When I try to read this file using stateFile.getInputStream() , it doesn't return any contents.当我尝试使用stateFile.getInputStream()读取此文件时,它不返回任何内容。 What am I doing wrong?我究竟做错了什么?

My test class is annotated like this我的测试类是这样注释的

@RunWith(SpringRunner.class)
@SpringBootTest

I can see that the code fails if I try with a incorrect file.如果我尝试使用不正确的文件,我可以看到代码失败。 So I think its seeing the file in classpath but for some reason not reading contents.所以我认为它在类路径中看到了文件,但由于某种原因没有读取内容。

I just ran into this.我刚遇到这个。 I'm using Maven.我正在使用 Maven。 I took a look at my target/test-classes folder and my resource file wasn't in there (even though it was in my src/test/resources folder).我查看了我的 target/test-classes 文件夹,我的资源文件不在那里(即使它在我的 src/test/resources 文件夹中)。

I ran mvn clean install and then rechecked my target/test-classes folder and the resource file was now there.我运行了mvn clean install然后重新检查了我的目标/测试类文件夹,资源文件现在就在那里。 After that, my test was able to find the file and the test worked.之后,我的测试能够找到该文件并且测试成功了。

So it seems that your resources aren't copied until you do a mvn clean .因此,在您执行mvn clean之前,您的资源似乎不会被复制。 JUnit is looking in the classpath built by maven and until the file actually makes it into the target/test-classes folder, JUnit won't be able to find it. JUnit 正在查找 maven 构建的类路径,直到该文件真正进入 target/test-classes 文件夹,JUnit 才能找到它。

You cant access a @Value resource unless its a property defined.除非定义了属性,否则您无法访问 @Value 资源。 It should be this way.应该是这样。

@Value("${stateJsonPath}")
Resource stateFile;

If you have to get the resource from hardcoded path then use this way.如果您必须从硬编码路径获取资源,请使用这种方式。

Resource stateFile = new ClassPathResource("state.json");

This should simply work, notice that the path starts with a dot, indicating current directory;这应该很简单,注意路径以点开头,表示当前目录;

@Test   
public void testFile() {
    String path = "./src/test/resources";
    String fileName = "test.zip";

    File file = new File(path, fileName);

    assertTrue(file.exists());
}

I'm trying to read a file from classpath like this in my unit test:我正在尝试在单元测试中从classpath读取文件:

@Value("classpath:state.json")
Resource stateFile;

I have state.json file in src/test/resources directory.我在src/test/resources目录中有state.json文件。 When I try to read this file using stateFile.getInputStream() , it doesn't return any contents.当我尝试使用stateFile.getInputStream()读取此文件时,它不返回任何内容。 What am I doing wrong?我究竟做错了什么?

My test class is annotated like this我的测试课是这样注释的

@RunWith(SpringRunner.class)
@SpringBootTest

I can see that the code fails if I try with a incorrect file.如果我尝试使用不正确的文件,我可以看到代码失败。 So I think its seeing the file in classpath but for some reason not reading contents.因此,我认为它在类路径中看到文件,但由于某种原因未读取内容。

Sharing the working solution for posterity.为后代分享工作解决方案。 Files present in the classpath can be read using org.springframework.core.io.ResourceLoader .可以使用org.springframework.core.io.ResourceLoader读取类路径中存在的文件。 For instance, I have a sample data file called posts.json under directory src/test/java/resources/data and I have to load it during the test case execution as part of @Before例如,我在src/test/java/resources/data目录下有一个名为posts.json的示例数据文件,我必须在测试用例执行期间作为@Before一部分加载它

@ActiveProfiles("test")
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SampleTest {

    @Autowired
    private ResourceLoader resourceLoader = null;

    @BeforeEach
    void init() {
        File dataFile = resourceLoader.getResource("classpath:data/posts.json").getFile();
        ...
        ...
    }

}

NOTE : the file should present in the classpath when you use the classifier classpath:注意:当您使用分类器classpath:路径时,该文件应出现在类classpath:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM