如何将本机库路径添加到 JUNIT 任务?

2022-09-04 20:14:10

我有一个使用此驱动程序进行串行通信的Java项目。驱动程序使用 Windows 下的 dll 来创建串行端口。

该项目包含几个 JUnit 测试,这些测试使用“以 -> 运行 JUnit 测试”成功完成。但是,引用本机库的测试在运行 ant 时失败(并且不引用本机库传递的测试)。

到目前为止,我最好的猜测是将包含本机库的目录添加到java.library.path,但是我还没有通过构建.xml文件成功地做到这一点。

有人能说出一个(干净的)解决方案吗?

这是我的构建.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>

下面是测试报告的一部分(XML 格式):

    <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.&lt;init&gt;(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.&lt;init&gt;(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)

答案 1

Ant 中的 junit 任务允许设置系统属性,就像其他一些任务一样。您需要将嵌套元素中的值指定为:java.library.pathsysproperty

<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>

答案 2

使用 jvmarg 设置库加载路径:

<junit>
  <jvmarg value="-Djava.library.path=/blah/YOURPATH"/>

如果要将目录添加到现有路径,则需要使用 Ant 使用环境变量的功能

<property environment="env"/>
<junit>
  <jvmarg value="-Djava.library.path=${env.path}${path.separator}/blah/PATH"/>

推荐