You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by Wa...@emc.com on 2007/05/09 22:33:12 UTC

[SOLVED] RE: How do I pass a username/password to a Junit test

That did it. I thought that I had tried that, but without knowing which
approach to take it's difficult to spend time debugging any one
approach.

Junit code:

    @BeforeClass
    public static void initClass() {
        dbusr = System.getProperty("dbusr");
        dbpwd = System.getProperty("dbpwd");
        dburl = System.getProperty("dburl");

        if ((dbusr == null) || (dbpwd == null) || (dburl == null)) {
            throw new RuntimeException("Invalid configuration. Make sure
to set dbusr, dbpwd, and dburl!");
        }
    }

POM.XML:

      <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
           <systemProperties>
              <property>
                 <name>dbusr</name>
                 <value>${dbusr}</value>
              </property>
              <property>
                 <name>dbpwd</name>
                 <value>${dbpwd}</value>
              </property>
              <property>
                 <name>dburl</name>
                 <value>${dburl}</value>
              </property>
          </systemProperties>
         </configuration>
       </plugin>

and the command line:

mvn -D dbusr=lester -D dbpwd=youwish -D
dburl=jdbc:oracle:thin:@//localhost:1521/xe test

Thanks for the help.

-- Les Walker

-----Original Message-----
From: Wayne Fay [mailto:waynefay@gmail.com] 
Sent: Wednesday, May 09, 2007 3:04 PM
To: Maven Users List
Subject: Re: How do I pass a username/password to a Junit test

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, Walker_Les@emc.com <Wa...@emc.com> 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: users-unsubscribe@maven.apache.org
> For additional commands, e-mail: users-help@maven.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org