Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing java system properties to ant tests

Tags:

java

ant

I have ant executing a jar with the following code

  <target name="start.my.jar" description="start my jar">
  <echo message="Starting the jar" />
  <java jar="${jars.dir}/${my.stub.jar}" fork="true" dir="${jars.dir}" spawn="true">
            <sysproperty key="properties.filename" value="${basedir}/path/path/path/filename.properties"/>
        <arg value="start" />
    </java>
</target>

inside the jar there is a class with the following code

public static MyFacade createFacade() throws FileNotFoundException, IOException {
    return createFacade(System.getProperty(properties.filename));
}    

and then there's an ant test goal that is configured as this

 <target name="test" depends="compile, support" description="perform unit tests">
    <mkdir dir="${build.test}" />
    <mkdir dir="${test-classes.dir}" />
    <javac srcdir="${build.test}" destdir="${test-classes.dir}" debug="${debug}" nowarn="${nowarn}" includeantruntime="false" deprecation="${deprecation}">
        <classpath refid="main.classpath" />
    </javac>
    <junit printsummary="yes" haltonfailure="yes" showoutput="true" dir="${test-classes.dir}" fork="true" forkmode="perBatch" failureproperty="junit.failure" errorproperty="junit.error" haltonerror="no">
        <jvmarg value="-Xmx1G" />
        <jvmarg value="-Dcom.sun.management.jmxremote" />

        <classpath>
            <pathelement location="${test-classes.dir}" />
            <pathelement location="${classes.dir}" />
            <!--
            For module restful_api
            -->
            <pathelement location="${build.deploy}" />
            <pathelement location="${classes.dir}" />
            <fileset dir="${build.lib}">
                <include name="*.jar" />
            </fileset>
        </classpath>
        <formatter type="plain" />
        <batchtest fork="yes" todir="${build.test}">
            <fileset dir="${build.test}">
                <include name="**/*AllTests.java" />
                <include name="**/*TestCase.java" />
            </fileset>
        </batchtest>
    </junit>
    <fail message="Unittest failures - please check" if="junit.failure" />
    <fail message="Unittest errors - please check" if="junit.error" />
</target>

My tests in this test target module fail to get the property for the file i specified on the start.my.jar target. Is there anything I'm doing wrong?

SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
[junit] java.lang.NullPointerException
[junit]     at java.io.File.<init>(File.java:222)
[junit]     at com.mycompany.myproduct.sdk.facade.MyFacadeFactory.getInputStream(MyFacadeFactory.java:47)
[junit]     at com.mycompany.myproduct.sdk.facade.MyFacadeFactory.loadFacade(MyFacadeFactory.java:43)
[junit]     at com.mycompany.myproduct.sdk.facade.MyFacadeFactory.createFacade(MyFacadeFactory.java:32)
[junit]     at com.mycompany.myproduct.sdk.facade.MyFacadeFactory.createFacade(MyFacadeFactory.java:28)
[junit]     at com.mycompany.myproduct.sdk.resources.impl.TransactionResourceImpl.<init>(TransactionResourceImpl.java:70)
[junit]     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
[junit]     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
[junit]     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
[junit]     at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
[junit]     at com.sun.jersey.server.spi.component.ResourceComponentConstructor._construct(ResourceComponentConstructor.java:191)
like image 263
ThaSaleni Avatar asked Nov 19 '12 09:11

ThaSaleni


1 Answers

That's because you add the property for only the java target, which is an independent runtime environment. The junit target specifies a new environment (as you set some JVM switches for it, you need to specify the system properties too).

Try this:

<junit ...>
    <sysproperty key="properties.filename"
            value="${basedir}/path/path/path/filename.properties"/>
    ....
</junit>

Another alternative is to run the ant task with the -Dproperties.filename=... key every time (you can set that in the external run configurations in Eclipse). The drawback is however, you have to remember this every time you want to run the task (e.g., in a CI build or with a fresh checkout).

like image 148
rlegendi Avatar answered Sep 28 '22 06:09

rlegendi