Get list of activated profile name during run time in maven java project
Asked Answered
H

2

16

I need to be able to use the profile activated during the run time of JUnit tests. I was wondering if there is any way of doing something like:

String str = System.getProperty("activated.profile[0]");

Or any other relative way...

I realized there is an option to use ${project.profiles[0].id} bu somehow it's not working.

Any ideas?

Hudibrastic answered 31/12, 2015 at 14:46 Comment(5)
Why do you need the information about a profile in a unit test?Jadwigajae
This really sounds like a typical XY problem. What are you really trying to acheive here?Eustace
i've been using profiling for different categories for running junit tests suite. i need to determine if the suite running is in scope of Sanity, regression, slowTests, etc and this one i'd like to get from the profile id by kind of: System.getProperty(profiles).contains("Sanity"). i wanted to avoid using properties in pom cause it will make me to duplicate some lines which i don't like.Hudibrastic
I don't think there is an easy and elegant way to do this. If I make an attempt to be a bit creative here, how about, having a specific file (property file), as a test resource, that would contain one value (a property) which can be replaced (resource replace) before running the tests . Each profile could set the property value to something that makes sense for you and then your test code could look up this file and read this value, in the target folder.Laquitalar
I just thought there is an easy way to get the I'd of the current activated profile.Hudibrastic
T
21

When using surefire to run the unit tests, it usually spawns a new JVM to run the tests, and we have to pass the information to the new JVM. This can be done usually using the "systemPropertyVariables" tag.

I was able to exercise this using a quickstart Java project, where I added this to the POM:

I declared the following profiles

<profiles>
    <profile>
        <id>special-profile1</id>
    </profile>
    <profile>
        <id>special-profile2</id>
    </profile>
</profiles>     

And this to surefire configuration:

<build>
    <plugins>
        ...
        <plugin>
           <groupId>org.apache.maven.plugins</groupId>
           <artifactId>maven-surefire-plugin</artifactId>
           <version>2.19</version>
           <configuration>
               <systemPropertyVariables>
                   <profileId>${project.activeProfiles[0].id}</profileId>
               </systemPropertyVariables>
           </configuration>
        </plugin>
        ...
    </plugins>
</build>  

And in my unit test, I added this:

/**
 * Rigourous Test :-)
 */
public void testApp()
{
    System.out.println("Profile ID:  " + System.getProperty("profileId"));
}

When invoking the "test" command without profile (i.e. using mvn test), I got this:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.fxs.AppTest
Profile ID:  development
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec - in com.fxs.AppTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

And we I used mvn -P special-profile2 test, I got this

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.fxs.AppTest
Profile ID:  special-profile2
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec - in com.fxs.AppTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

This will pass along the name of the first active profile. If we have potentially more than one active profiles, then we will probably need to use more system properties.

Note: I tested this using Maven 3.1.1

Tuxedo answered 31/12, 2015 at 20:54 Comment(2)
This is very close to what I'm looking for. Can I : /** public void testApp() { System.out.println("Profile ID: " + System.getProperty("project.activeProfile[0].id")); } when I tried this, it gave me a null when running it from intelijHudibrastic
I don't think you can reference projcect.activeProfile[0].id like this from within your unit test. When the test is executing, it is run by a new JVM that was started by the Surefire test plugin from maven. This new JVM doesn't see what was visible in the parent JVM where maven is running. (in addition, to reference the data model with expressions like this, would require an expression evaluator, which is only available in maven). AFAIK, the only way to pass thing to child JVM is by using <systemPropertyVariables>.Tuxedo
C
-2

I other cases I used next in pom file:

<profiles>
    <profile>
        <id>a-profile-id</id>

        <properties>
            <flag>a-flag-value</flag>
        </properties>
    </profile>
</profiles>

and in java:

String flagValue = System.getenv("flag");
Covington answered 25/9, 2019 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.