java – 如何添加本地库路径到JUNIT任务?

前端之家收集整理的这篇文章主要介绍了java – 如何添加本地库路径到JUNIT任务?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 Java项目,使用 this driver进行串行通信.驱动程序使用Windows下的dll来创建串行端口.

该项目包含几个JUnit测试,使用“运行为 – > JUnit测试”成功完成.但是,当运行ant(和不引用本机库通过的测试)时,引用本机库的测试失败.

到目前为止,我最好的猜测是将包含本机库的目录添加到java.library.path中,但是我没有通过build.xml文件成功.

有人可以告诉(干净)解决方案吗?

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

这里是测试报告的一部分(以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)

解决方法

junit task in Ant,允许设置系统属性,就像其他一些任务一样.您需要在sysproperty嵌套元素中指定java.library.path值:
<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>
原文链接:https://www.f2er.com/java/126183.html

猜你在找的Java相关文章