4

I am using eclipse with maven for a mobile automation test on a mobile webpage.

I am defining the following in my pom.xml

<properties>
    <MY_VARIABLE>www.google.com/</MY_VARIABLE>
</properties>

but when i am calling this using

String testurl1 = System.getProperty("MY_VARIABLE");

it always seems to return null.

I also tried the following way of defining variable

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.16</version>
    <configuration>
        <systemPropertyVariables>
        <MY_VARIABLE>www.google.com</MY_VARIABLE>
        </systemPropertyVariables>
    </configuration>
</plugin>

but still am getting the value as null.

I could use some help Thanks.

Ram
  • 405
  • 1
  • 6
  • 13
  • You should use a property file for that –  Oct 20 '14 at 07:15
  • I think there is a mixup of variable names. The property you use us MY_VARIABLE but in the surefire plugin config its MY_URL / MY_VARIABLE? The way you have with the surefire plugin should work, you may just use the wrong name? – wemu Oct 20 '14 at 07:18
  • @wemu i am not using MY_VARIABLE twice in my code anywhere either the first method or the second method... – Ram Oct 20 '14 at 07:27
  • @RC. could you explain a little further .. – Ram Oct 20 '14 at 07:27
  • just the xml you have there cannot be valid: www.google.com – wemu Oct 20 '14 at 07:29
  • @wemu sorry that was a typo.. – Ram Oct 20 '14 at 07:31
  • no problem. just wanted to make sure its not a typo issue :) this may be related: http://stackoverflow.com/questions/3231797/specify-system-property-to-maven-project – wemu Oct 20 '14 at 08:10

1 Answers1

6

Your configuration will not work in eclipse since there is no good m2e support for surefire. The maven surefire plugin forkes a new process and provides the systemPropertyVariables to it. Your configuration will work if you run the tests from the command-line, e.g.

mvn surefire:test

To make it run in both worlds (command-line and eclipse) I do it this way...

  1. Create a src/test/resources/maven.properties
  2. Edit the maven.properties file and put the properties you need in it, e.g.

    project.build.directory=${project.build.directory}
    MY_VARIABLE=${MY_VARIABLE}
    
  3. Enable resource filtering for test resources

    <build>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
        ...
    </build>
    
  4. Load the properties in your test and access them

     Properties mavenProps = new Properties();
     InputStream in = TestClass.class.getResourceAsStream("/maven.properties");
     mavenProps.load(in);
    
     String buildDir = mavenProps.getProperty("project.build.directory");
     String myVar = mavenProps.getProperty("MY_VARIABLE");
    
René Link
  • 48,224
  • 13
  • 108
  • 140
  • so i have to add just this line in the maven.properties file MY_VARIABLE = www.google.com and how do i call the variable after mavenProps.load(in); command ... – Ram Oct 20 '14 at 07:30
  • thanks it worked just one more question for you i can just use the MY_VARIABLE from the maven.properties in my pom.xml by just ${MY_VARIABLE} right ? – Ram Oct 20 '14 at 07:37
  • @Ram Yes that's right, because when maven filters the `maven.properties` file it replaces all occurrences of `${...}` with the value of the property. Take a look at http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html for more info. – René Link Oct 20 '14 at 07:40
  • Could you tell me how do i call a variable that i have defined in the surefire plugin is it same as mavenProps.get() because System.getPropoerty() does not work – Ram Oct 20 '14 at 08:49
  • I updated my answer in order to explain why it does not work when you start the junit tests in eclipse. – René Link Oct 22 '14 at 06:06
  • I was able to access the properties in Eclipse while running JUnit tests. I just added a line to my java code see it here [link](http://stackoverflow.com/questions/26464371/java-unable-to-read-variables-from-pom) – Ram Oct 22 '14 at 07:05