Spring + Maven:为单元测试和集成测试分离属性文件

时间:2021-01-27 15:38:14

I'm using Spring 2.5.6 and building my project with Maven 2.2.1. We use PropertyPlaceholderConfigurer beans in Spring to load up properties for configuring things like the database. Pretty standard stuff. We also have two different sets of tests: unit tests and integration tests.

我正在使用Spring 2.5.6并使用Maven 2.2.1构建我的项目。我们在Spring中使用PropertyPlaceholderConfigurer bean来加载用于配置数据库等内容的属性。非常标准的东西。我们还有两组不同的测试:单元测试和集成测试。

I would like to be able to use different property files to configure things like database url differently for the two different types of tests. For example, I want unit tests to use the localhost database and integration tests to use the mydatabase.example.com database.

我希望能够使用不同的属性文件为这两种不同类型的测试配置不同的数据库url。例如,我希望单元测试使用localhost数据库和集成测试来使用mydatabase.example.com数据库。

I have tried several variations where I place the property files in separate subdirectories (one for unit tests and one for integration tests). From there, I've tried things like using the additionalClasspathElements tag for the maven-failsafe-plugin, but that didn't seem to work at all. I tried using the maven-antrun-plugin to copy the files into target/classes, but that didn't get triggered when I ran mvn verify -Dtest=sometest.

我尝试过几种方法,将属性文件放在不同的子目录中(一个用于单元测试,一个用于集成测试)。从那里开始,我尝试了一些方法,比如使用maven- failsecurityplugin的additionalclasspathelement标签,但这似乎根本不起作用。我尝试使用maven-antrun-plugin将文件复制到目标/类中,但是当我运行mvn验证-Dtest=sometest时,这并没有被触发。

I also tried using systemPropertyVariables in maven to set a property called buildEnvironment, which I then tried to reference in my Spring bean definition:

我还尝试在maven中使用systemPropertyVariables来设置一个名为buildEnvironment的属性,然后在我的Spring bean定义中尝试引用这个属性:

<property name="locations">
  <value>classpath:${buildEnvironment}/my-test.properties</value>
</property>

But Spring refused to resolve ${buildEnvironment}. At this point I'm out of ideas. I'm sure there's a nice, straightforward way to do this, but I can't figure it out.

但是Spring拒绝解决${构建环境}。在这一点上我没有主意了。我确定有一个很好的,简单的方法,但是我不能算出来。

Any advice would be greatly appreciated.

如有任何建议,我们将不胜感激。

1 个解决方案

#1


1  

You could enable resource filtering and create maven properties:

您可以启用资源筛选并创建maven属性:

<build>
  <resources>
    <resource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

<properties>
   <buildEnvironment>yourValue</buildEnvironment>
</properties>

${buildEnvironment} in your Spring config will then be resolved to "yourValue" (assuming it is under src/test/resources/)

在您的Spring配置中,${buildEnvironment}将被解析为“yourValue”(假设它在src/test/resources/)中。

#1


1  

You could enable resource filtering and create maven properties:

您可以启用资源筛选并创建maven属性:

<build>
  <resources>
    <resource>
      <directory>src/test/resources</directory>
      <filtering>true</filtering>
    </resource>
  </resources>
</build>

<properties>
   <buildEnvironment>yourValue</buildEnvironment>
</properties>

${buildEnvironment} in your Spring config will then be resolved to "yourValue" (assuming it is under src/test/resources/)

在您的Spring配置中,${buildEnvironment}将被解析为“yourValue”(假设它在src/test/resources/)中。