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?

Mickey Hovel
  • 982
  • 1
  • 15
  • 31
  • Why do you need the information about a profile in a unit test? – khmarbaise Dec 31 '15 at 14:49
  • 1
    This really sounds like a typical XY problem. What are you really trying to acheive here? – Tunaki Dec 31 '15 at 15:02
  • 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. – Mickey Hovel Dec 31 '15 at 15:10
  • 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. – javapapo Dec 31 '15 at 17:15
  • I just thought there is an easy way to get the I'd of the current activated profile. – Mickey Hovel Jan 01 '16 at 21:53

2 Answers2

18

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

fshehadeh
  • 428
  • 1
  • 4
  • 12
  • 1
    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 intelij – Mickey Hovel Jan 01 '16 at 21:47
  • 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 ``. – fshehadeh Jan 02 '16 at 16:01
-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");
Horatiu
  • 375
  • 3
  • 6