使用本机库进行Surefire JUnit测试 [英] Surefire JUnit Testing using Native Libraries
问题描述
我们在哈德森使用Maven运行Java构建过程,并使用Surefire插件执行JUnit测试,但是我遇到了一个需要本地dll的项目的单元测试问题.
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.
我们看到的错误是:
测试错误:TestFormRegistrationServiceConnection(com.#productidentifierremoved#.test.RegistrationServiceTest):java.library.path中没有身份验证器
Tests in error: TestFormRegistrationServiceConnection(com.#productidentifierremoved#.test.RegistrationServiceTest): no Authenticator in java.library.path
其中Authenticator是我们需要的dll的名称.
我发现
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>
但是,如果我们包含System.out.println(System.getProperty("java.library.path")),这仍然会产生相同的错误.我们可以看到它没有被添加到路径中.
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?
推荐答案
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>
更新:
好吧,似乎必须在具有JUnit测试的JVM启动之前设置此属性.因此,我想您在反斜杠方面遇到了问题. Java属性值中的反斜杠用于转义特殊字符,例如\t
(制表符)或\r\n
(Windows新行).因此,请尝试使用此方法代替您的解决方案:
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>
这篇关于使用本机库进行Surefire JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!