9

我们在 Hudson 中使用 Maven 来运行我们的 Java 构建过程,并使用 Surefire 插件来执行 JUnit 测试,但是我在一个需要本机 dll 的项目的单元测试中遇到了问题。

我们看到的错误是:

测试错误:TestFormRegistrationServiceConnection(com.#productidentifierremoved#.test.RegistrationServiceTest): no Authenticator in java.library.path

其中 Authenticator 是我们需要的 dll 的名称。我发现这个 SO post表明设置它的唯一方法是通过 argLine。我们将配置修改为:

    <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")); 我们可以看到这没有被添加到路径中。

有什么想法可以解决这个问题吗?

4

1 回答 1

10

要将系统属性添加到 JUnit 测试,请配置Maven Surefire 插件,如下所示:

<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(tabulator) 或\r\n(windows new-line)。因此,请尝试使用它而不是您的解决方案:

<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>
于 2012-04-19T10:32:56.847 回答