3

I create tests using Selenium WebDriver and Cucumber-jvm, built on Maven. I want to achieve next:

I want to have profiles with properties and use this properties in my steps depended on enviroments.

I've created a folder in src/test/resources and added 2 subfolder in it: Staging and Dev.

In each folder I have a file config.properties where I have saved username.

My POM looks like :

<profiles>
    <profile>
        <id>staging</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>

        </properties>
    </profile>

    <profile>
        <id>dev</id>
        <properties>

        </properties>
    </profile>
</profiles>

Now I want to change properties of profiles to something like this:

<properties> test/resources/dev/config.properties</properties>
<properties> test/resources/staging/config.properties</properties>

And I want when I run my test with an active staging profile in my step defs when I call:

system.getProperty("username")

I want this to return username which is provided in staging's properties.

When I run this when dev profile is active, I want to get dev's property.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
SornoDavid
  • 47
  • 1
  • 7
  • You say "_I've created a folder in `src/test/resources` and added 2 subfolder in it: `Staging` and `Dev`._" and later "_` test/resources/[dev|staging]/config.properties`_". Where is this folder in the latter and what are the sub-folders' real names, with leading capitals or not? – Gerold Broser Oct 27 '18 at 05:58

2 Answers2

2
  • Add properties to your profiles, e.g:

    <propertiesFile>staging.properties</propertiesFile>
    


    <propertiesFile>dev.properties</propertiesFile>
    
  • Name the different properties files accordingly and place them in src/test/resources directly.
  • Copy the according properties file to config.properties with one of the options described in Best practices for copying files with Maven by using ${propertiesFile}. I prefer the Wagon Maven Plugin.

UPDATE

That means: Forget about the two extra directories containing the two properties files. Put them both in src/test/resources/ and copy them according to:

staging

src/test/resources/staging.properties copied to:

  • src/test/resources/config.properties
  • or to target/config.properties

depending on the phase you bind the copy process to.

dev

src/test/resources/dev.properties copied to:

  • src/test/resources/config.properties
  • or to target/config.properties

depending on the phase you bind the copy process to.

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • hi. I don't understand your second step. I have two directories - src\test\resources\configs\staging\config.properties - for staging enviroment and -src\test\resources\configs\dev\config.properties - for dev enviroment. I want the next - when I execute from comand line -Pdev which activate dev profile, I want in my code system.getProperty() to return properties from dev configs. When I activate staging profile, I want to get properties from staging configs. How Can I achieve it? – SornoDavid Oct 27 '18 at 07:52
  • @SornoDavid I know what you want from your question. See the update to my answer. – Gerold Broser Oct 27 '18 at 17:21
  • Now I understang what you suggest.But I don't want to use copy or something.I'l mark this answer as right answer,because It works. But I'm trying to change and copy properties files automatically. I saw this link - https://www.petrikainulainen.net/programming/tips-and-tricks/creating-profile-specific-configuration-files-with-maven/ which might helps me. Thank you for your suggestion – SornoDavid Oct 28 '18 at 08:04
  • @SornoDavid What do you mean by "_automatically_"?. Nothing happens automatically in IT unless you implement it. And I gave you a link in my answer that shows a few ways how to implement exactly what you want. What's wrong with copying? BTW, since you're new to SO: Do you know that you can show additional appreciation by also upvoting an accepted answer, see also [Why is voting important?](https://stackoverflow.com/help/why-vote). – Gerold Broser Oct 28 '18 at 09:33
  • in automatically I mean that I won't have to copy something from something to something. I want when I build my project, my project would be able to deside which properties are needed for the build. Again,thanks for your answer.I tried upvoteing when I accepted this post as answer,but I'm not able as my reputation is under 15.Thank you again – SornoDavid Oct 28 '18 at 10:33
  • @SornoDavid I see. I wasn't aware of this 15 rep limit. Now you are able to. :) – Gerold Broser Oct 28 '18 at 12:00
0

My solution is like this:

  1. Use Spring Boot external configuration. We could set up either application-{profile}.properties or application-{profile}.yaml according to your preference. Put the environment variables (e.g. hostname, uri, credentials etc) over there. And if we need variables for all the environments, just put them in application.properties or application.yaml.

  2. Use the maven argument spring.profiles.active to activate the profile accordingly. The following command is one use case in CI/CD:

    mvn clean verify -Dspring.profiles.active={profile} ..........

Dharman
  • 30,962
  • 25
  • 85
  • 135
Eric Tan
  • 1,377
  • 15
  • 14