简体   繁体   中英

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. When I try to read this file using stateFile.getInputStream() , it doesn't return any contents. 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. 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).

I ran mvn clean install and then rechecked my target/test-classes folder and the resource file was now there. 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 . 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.

You cant access a @Value resource unless its a property defined. 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:

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

I have state.json file in src/test/resources directory. When I try to read this file using stateFile.getInputStream() , it doesn't return any contents. 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 . 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

@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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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