Java – how to add local library paths to JUnit tasks?
I have a java project that uses this driver for serial communication The driver uses DLL under windows to create serial port
The project contains several JUnit tests, which were successfully completed using "run as – > JUnit tests" However, when ant is run (and tests that do not reference native libraries pass), tests that reference native libraries fail
So far, my best guess is to add a directory containing native libraries to Java library. Path, but I didn't pass build XML file succeeded
Can anyone tell me the (clean) solution?
This is my build xml:
<path id="compile.classpath"> <fileset dir="${lib}"> <include name="**/*.jar"/> </fileset> <fileset dir="${junit_home}"> <include name="**/*.jar"/> </fileset> </path> <path id="test.classpath"> <pathelement location="${bin}" /> <fileset dir="${lib}"> <include name="**/*.jar"/> </fileset> <fileset dir="${junit_home}"> <include name="**/*.jar"/> </fileset> </path> <target name="compile"> <mkdir dir="${bin}" /> <echo Message="Compiling src folder..." /> <javac includeantruntime="no" classpathref="compile.classpath" srcdir="${src}" destdir="${bin}" /> <echo Message="Compiling test folder..." /> <javac includeantruntime="no" classpathref="compile.classpath" srcdir="${test}" destdir="${bin}" /> </target> <target name="test"> <mkdir dir="${test.reports}" /> <junit fork="yes" printsummary="yes" haltonfailure="yes"> <test name="${test.class.name}" todir="${test.reports}" /> <formatter type="xml" /> <classpath refid="test.classpath" /> </junit> </target>
Here is a part of the test report (in XML format):
<testcase classname="nl.timo.comport.test.buildservertests.ComportFactoryTest" name="testGetInstance" time="0.0" /> <testcase classname="nl.timo.comport.test.buildservertests.ComportFactoryTest" name="testCreateDefaultComport" time="0.016"> <error message="giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;" type="java.lang.UnsatisfiedLinkError">java.lang.UnsatisfiedLinkError: giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String; at giovynet.nativelink.SerialPort.getStateSerialPortC(Native Method) at giovynet.nativelink.SerialPort.getFreeSerialPort(SerialPort.java:50) at package.comport.GioComport.getFreeSerialPorts(UnkNown Source) at package.comport.GioComport.findDevice(UnkNown Source) at package.comport.GioComport.<init>(UnkNown Source) at package.comport.ComportFactory.createNewPort(UnkNown Source) at package.comport.ComportFactory.createComport(UnkNown Source) at package.comport.test.buildservertests.ComportFactoryTest.testCreateDefaultComport(UnkNown Source) </error> </testcase> <testcase classname="nl.timo.comport.test.buildservertests.ComportFactoryTest" name="testCreateComportWithWrongSettings" time="0.0"> <error message="giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String;" type="java.lang.UnsatisfiedLinkError">java.lang.UnsatisfiedLinkError: giovynet.nativelink.SerialPort.getStateSerialPortC(Ljava/lang/String;)Ljava/lang/String; at giovynet.nativelink.SerialPort.getStateSerialPortC(Native Method) at giovynet.nativelink.SerialPort.getFreeSerialPort(SerialPort.java:50) at package.comport.GioComport.getFreeSerialPorts(UnkNown Source) at package.comport.GioComport.findDevice(UnkNown Source) at package.comport.GioComport.<init>(UnkNown Source) at package.comport.ComportFactory.createNewPort(UnkNown Source) at package.comport.ComportFactory.createComport(UnkNown Source) at package.comport.test.buildservertests.ComportFactoryTest.testCreateComportWithWrongSettings(UnkNown Source) </error> </testcase> <system-out><![CDATA[]]></system-out> <system-err><![CDATA[java.lang.UnsatisfiedLinkError: no libSerialPort in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
Solution
JUnit task in ant allows you to set system properties, just like other tasks You need to specify Java. Java in the sysproperty nested element library. Path value:
<junit fork="yes" printsummary="yes" haltonfailure="yes"> <test name="${test.class.name}" todir="${test.reports}" /> <formatter type="xml" /> <classpath refid="test.classpath" /> <sysproperty key="java.library.path" value="put your library path here"/> </junit>