删除了Spring Boot版本2.3.x src / test / resources

问题描述

在Springboot版本2.3.x中,似乎已删除src / test / resources文件夹,

我从start.spring.io下载了一个框架,但没有看到它,当前的项目结构如下,在这种情况下,如何使用测试专用的属性文件,即使创建了src /在test / resources文件夹中并放入一个test.property文件,在我运行测试时找不到该文件,非常感谢您的帮助。

enter image description here

我在start.spring.io中生成框架项目时选择的选项

enter image description here

解决方法

您正在使用什么IDE?项目是否使用Maven?在带有Maven的Eclipse中,如果右键单击src / test并选择New-> Source Folder,然后将其称为资源,则应由Maven创建并添加到测试类路径中。

enter image description here

将适当的文件放在src / main / resources和src / test / resources中并运行后

    try(final Stream<String> fileStreamFromClasspath = Files.lines(Path.of(ClassLoader.getSystemResource("test.file").toURI())))
    {
        fileStreamFromClasspath.forEach(l -> System.out.println(l)); // NOSONAR
    }
    try(final Stream<String> fileStreamFromFilesystem = Files.lines(Path.of(("src/main/resources/test.file"))))
    {
        fileStreamFromFilesystem.forEach(l -> System.out.println(l)); // NOSONAR
    }
    try(final Stream<String> testFileStreamFromFilesystem = Files.lines(Path.of(("src/test/resources/test.file"))))
    {
        testFileStreamFromFilesystem.forEach(l -> System.out.println(l)); // NOSONAR
    }

产生输出:

This is the test test file.
This is the main test file.
This is the test test file.
,

经过大量研究后,我找到了答案,因此src / test下的“ resources”文件夹已被删除,这是我们可以从start.spring.io下载的框架项目的一部分,但现在它们包含了一个新的构建任务,我的项目也处于gradle中,但是我确信Maven也是如此,gradle中此任务的名称为“ processTestResources”,它将src/test/resources文件夹中的所有内容复制到{ {1}}位于类路径中,您可以像以前一样访问资源,唯一的需要是现在您必须手动创建“ resources”文件夹,现在已解决问题,感谢大家的投入。

enter image description here

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...