Surefire JUnit Testing using Native Libraries

我是研究僧i 提交于 2019-12-01 14:40:58

问题


We are using Maven in Hudson to run our Java build process and the Surefire plugin to execute JUnit tests however I've run into a problem with the unit tests for one project which requires native dlls.

The error we're seeing is :

Tests in error: TestFormRegistrationServiceConnection(com.#productidentifierremoved#.test.RegistrationServiceTest): no Authenticator in java.library.path

Where Authenticator is the name of the dll we require. I found this SO post which suggest that the only way to set this is through argLine. We modified our config to this:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
        <version>2.10</version>
        <configuration>
            <forkMode>once</forkMode>
            <argLine>-Djava.library.path=${basedir}\src\main\native\Authenticator\Release</argLine>
        </configuration>
    </plugin>

However this still gives the same error and if we include a System.out.println(System.getProperty("java.library.path")); we can see that this is not being added to the path.

Any ideas how we can solve this?


回答1:


To add a system property to the JUnit tests, configure the Maven Surefire Plugin as follows:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <systemPropertyVariables>
          <java.library.path>${project.basedir}/src/main/native/Authenticator/Release</java.library.path>
        </systemPropertyVariables>
      </configuration>
    </plugin>
  </plugins>
</build>

Update:

Ok, it seems this property has to be set before the JVM with JUnit tests starts. So I guess that you have problem with the backslashes. Backslashes in the Java property value are used to escape special characters like \t (tabulator) or \r\n (windows new-line). So try to use this instead of your solution:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <forkMode>once</forkMode>
        <argLine>-Djava.library.path=${project.basedir}/src/main/native/Authenticator/Release</argLine>
      </configuration>
    </plugin>
  </plugins>
</build>


来源:https://stackoverflow.com/questions/10226082/surefire-junit-testing-using-native-libraries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!