简体   繁体   English

在测试时组合Application.properties文件

[英]combine Application.properties files when testing

I would like to know how can I append the content of all my Application.resources files into a single one when running the tests for my application? 我想知道在为我的应用程序运行测试时如何将所有Application.resources文件的内容附加到单个文件中?

Every time I run the tests, some configuration must be added from src/resources/Application.properties file to test/resource/Application.properties file. 每次运行测试时,必须从src/resources/Application.properties文件向test/resource/Application.properties文件添加一些配置。

How can I obtain this? 我怎么能得到这个?

I am using Spring and Gradle 我正在使用Spring和Gradle

It can be done eg in the following way (there's a need to append main resources content to test resources file, keep old test resources for test execution, then swap the files): 它可以通过以下方式完成(需要将主要资源内容附加到测试资源文件,保留旧测试资源以进行测试执行,然后交换文件):

apply plugin: 'java'

test {
   doFirst {
      def testRes = project.file('src/test/resources/test.resources')
      def mainRes = project.file('src/main/resources/main.resources')
      project.copy {
         from 'src/test/resources'
         into 'src/test/resources'
         include('test.resources')
         rename {
            'test.resources.old'
         }
      }
      testRes << '\n'
      testRes << mainRes.text

      logger.lifecycle('\nmain.resources text: ')
      logger.lifecycle(mainRes.text)
      logger.lifecycle('\ntest.resources text: ')
      logger.lifecycle(testRes.text)
      logger.lifecycle('\ntest.resources.old text: ')
      logger.lifecycle(project.file('src/test/resources/test.resources.old').text)
   }

   doLast {
      def testOldRes = project.file('src/test/resources/test.resources.old')
      testOldRes.renameTo(project.file('src/test/resources/test.resources'))

      logger.lifecycle('\nmain.resources text: ')
      logger.lifecycle(project.file('src/main/resources/main.resources').text)
      logger.lifecycle('\ntest.resources text: ')
      logger.lifecycle(project.file('src/test/resources/test.resources').text)
      logger.lifecycle("\ntest.resources.old exists: ${project.file('src/test/resources/test.resources.old').exists()}")
   }
}

Runnable demo can be found here . 可以在这里找到Runnable演示。

In Spring you can import the data from existing resources. 在Spring中,您可以从现有资源导入数据。 So basically, you need to create a Spring context file for your testing module and use the following tag to import the content of Application.properties from src/resources. 基本上,您需要为测试模块创建一个Spring上下文文件,并使用以下标记从src / resources导入Application.properties的内容。

<import resource="src/resources/*.properties"/>

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

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