This happens because Surefire by default forks "once" which means all
tests run in their own JVM.

If your tests will run with no forking, you can set
<forkMode>never</forkMode> and all properties passed on the mvn
command line will go straight through to the tests. Most people will
not choose this option.

So the next option is to pass the CLI values into variables in the
pom, and then pass them into the tests as system Properties, ie:

In test source code:
 String dbusr = System.getProperty("dbusr");
 System.out.println("dbusr is: " + dbusr);

In pom.xml:
<plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-surefire-plugin</artifactId>
       <configuration>
         <systemProperties>
           <property>
             <name>dbusr</name>
             <value>${foo}</value>
           </property>
         </systemProperties>
       </configuration>
     </plugin>

On command line:
C:\temp\blah>mvn test -Dfoo=blah
Running myTest
dbusr is: blah

Wayne

On 5/9/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
I've seen this question asked a few places before, but the only
responses that I've seen have been either dead links or links to
reference material that doesn't actually explain the "how" of it.

I have a unit test that connects to a database using a username and
password in a system property as described in the junit docs:
http://junit.sourceforge.net/doc/faq/faq.htm#running_7. I have a line of
code in my test setup that does:

dbusr = System.getProperty("dbusr");

It does the same for the db password and url.

We are using several IDE's and each test runner allows me to define this
property on the command line:

-Ddbusr=foo

However using this switch on the "mvn" command line does not set the
system property when the test is run.

Now I have been able to make this work by specifying the
<systemProperties> element in the pom.xml under the surefire plugin's
configuration. This will pass the hardcoded values from the pom.xml to
the unit test. However, I have not figured out a way to parameterize the
build so that users can supply this information themselves. To date I
have tried the following:

1) Specifying maven.junit.jvmargs=-D... on the command line (in quotes)
as well as in a project.properties file and hardcoded in the pom.xml
itself. This property seems to have no affect regardless of what I do
with it.
2) Specifying the properties themselves on the maven command line, in a
project.properties file.
3) Doing number (2) in conjunction with the maven.junit.sysproperties
set to the names of the properties on the command line, in a
project.properties file.
4) Setting maven.junit.sysproperties hardcoded in the pom.xml as a
system property to the surefire configuration and then setting the
properties themselves as in (2).

None of the 4 approaches above have worked. Has anyone been able to
accomplish this? If so could you describe your approach.

Thank you,

-- Les Walker


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to