在进行 Spring 启动 junit 测试时,无法从 src/test/resources 加载文件



我创建了一个文件夹 src/test/resources 并添加了一个文件 Test.json 并尝试像这样访问 junit test

@RunWith(SpringRunner.class)
@WebMvcTest(value = ReadController.class, secure = false)
public class ReadControllerTest {
@Value("${classpath:Test.json}")
Resource testFile;
@Test
public void test() throws Exception{

File file = testFile.getFile();
System.out.println(file.exists());
}   
}

当我运行此单元测试时,它尝试在类路径中查找文件,但那里不存在文件。 如何在类路径中添加此文件?

默认情况下,Spring boot 类路径将指向 src/main/resources,但您使用的是 src/test/resources。 尝试将 JSON 文件复制到 src/main/resources。

相关内容

最新更新