简体   繁体   English

测试时将使用src / main / resources和src / test / resources目录中的哪个application.yml

[英]Which application.yml that're in src/main/resources and src/test/resources directories will be used when testing

In a spring boot Maven project, there's a application.yml file in src/main/resources and src/test/resources respectively. 在Spring Boot Maven项目中,分别在src/main/resourcessrc/test/resources有一个application.yml文件。

When running test cases, it seems that testing will only use the file in src/test/resources directory.It won't load two files and override the properties in src/main/resources/application.yml . 运行测试用例时,似乎测试只会使用src/test/resources目录中的文件,它将不会加载两个文件并覆盖src/main/resources/application.yml的属性。 And if there isn't the file in src/test/resources directory, it will use the file in src/main/resources directory. 如果src/test/resources目录中没有该文件,它将使用src/main/resources目录中的文件。

How does spring boot to load the application.yml file in these two directories? Spring Boot如何在这两个目录中加载application.yml文件? What're the rules? 有什么规定? or What does this action depend on? 或此动作取决于什么?
Where can I look up more information about this online? 在哪里可以在线查找有关此信息的更多信息?

Example: 例:

└───spring-boot-main-test-application-yaml
    ├───pom.xml
    └───src
        ├───main
        │   ├───java
        │   ├───resources
        │       ├───application.yml
        │   
        ├───test
            ├───java
            ├───resources
                └─── application.yml

src/main/resource/application.yml

name: Phil(main)
sample:
  name: Andy(main)

src/test/resource/application.yml

name: Phil(test)
sample:
  name: Andy(test)

case1: src/main/resource/application.yml exists 情况1: src/main/resource/application.yml存在
output: Hello Phil(main),Andy(main) 输出:Hello Phil(main),Andy(main)

case2: src/main/resource/application.yml and src/test/resource/application.yml both exist 情况2: src/main/resource/application.ymlsrc/test/resource/application.yml都存在
output: Hello Phil(test),Andy(test) 输出:Hello Phil(测试),Andy(测试)

case3: src/main/resource/application.yml and src/test/resource/application.yml both exist and comment out name in src/test/resource/application.yml 情况3: src/main/resource/application.ymlsrc/test/resource/application.yml都存在,并在src/test/resource/application.yml注释掉name
output: 输出:
Error - java.lang.IllegalArgumentException: Could not resolve placeholder 'name' in value "${name}"

It seems that this test didn't read properties from src/main/resource/application.yml so it couldn't find out name property. 似乎此测试未从src/main/resource/application.yml读取属性,因此无法找到name属性。

Spring detects those two directories and like you said it's figuring out which configs to use based on presence and priority. Spring会检测到这两个目录,就像您说的那样,它会根据状态和优先级确定要使用的配置。 If you wanted spring to use a particular setting you should define spring profiles which can have specialized settings as well as extend default settings. 如果希望弹簧使用特定设置,则应定义spring profiles ,该spring profiles可以具有专门的设置以及扩展的默认设置。

I am working with yml file and make sure that these property files are loaded when the app is build, it is preferred to have the following config in pom.xml 我正在使用yml文件,并确保在构建应用程序时加载了这些属性文件,因此在pom.xml中最好具有以下配置

<build>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.yml</include>
                    <include>**/*.jks</include>

                </includes>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>${project.basedir}/src/test/resources</directory>
            </testResource>
        </testResources>
 ....
</build>

Make sure to run the build (if required start from scratch like deleting the repository folder inside .m2 - This might not be needed at all) Now check the target/classes and that should contain the files of all the specified types 确保运行构建(如果需要,请从头开始,例如删除.m2中的存储库文件夹-可能根本不需要)。现在检查target/classes ,其中应包含所有指定类型的文件

暂无
暂无

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

相关问题 应用程序(src / main)运行时,类路径中的src / test / resources中的文件是? - Are files in src/test/resources on classpath while application (src/main) is running? 让 Eclipse 使用 src/test/resources 而不是 src/main/resources - Make Eclipse use src/test/resources instead of src/main/resources Springboot - application-integration.properties 在 src/main/resources 下工作,但不在 src/test/resources 下工作 - Springboot - application-integration.properties working under src/main/resources, but not under src/test/resources src / main / resources中的FileNotFoundException - FileNotFoundException in src/main/resources Gradle和src / main / resources - Gradle and src/main/resources 如何从src / main / java中的src / test / resources中读取道具 - How to read props from src/test/resources in src/main/java Spring xml + JUnit-xml上下文文件在src / test / resources中不起作用,但是在src / main / resources中起作用 - Spring xml + JUnit - xml context file not working in src/test/resources but works in src/main/resources 告诉gradle从src / test / resources而不是src / main / resources中选择persistence.xml - Tell gradle to pick persistence.xml from src/test/resources instead of src/main/resources src / main / resources中的文件不在classpath中 - file in src/main/resources is not in classpath 如何在JUnit测试中从src / main / resources中读取文件? - How to read file from src/main/resources relative in a JUnit test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM