我创建了一个文件夹 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。