如何在junit测试用例中设置env变量?

时间:2013-07-11 06:16:53

标签: maven guice env

我跟着How to set env variables for maven to run test correctly?并按照以下方式配置了我的pom:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.15</version>
   <configuration>
       <systemPropertyVariables>
           <SENDGRID_USERNAME>username</SENDGRID_USERNAME>
           <SENDGRID_PASSWORD>password</SENDGRID_PASSWORD>
       </systemPropertyVariables>
   </configuration>
</plugin>

当我运行mvn test命令时,它发出以下错误,

Error in custom provider, java.lang.IllegalStateException: SENDGRID_PASSWORD env variable must be set.

我做错了什么?

当我进行研究以找到答案时,我发现了以下内容:ProcessBuilder可以使用set env来生成使用它的进程。我不知道ProcessBuilder是否对我的情况有帮助,因为我没有产生任何进程。我的项目是maven项目,我使用mvn test命令运行测试。

任何机构都可以解释我如何为这种情况设置环境(即maven + junit)

我可以设置properties,但问题是如何设置environment variable,因为我的程序期望env变量。

1 个答案:

答案 0 :(得分:2)

如果我正确阅读文档,则使用的配置为:

 <environmentVariables>
       <SENDGRID_USERNAME>username</SENDGRID_USERNAME>
       <SENDGRID_PASSWORD>password</SENDGRID_PASSWORD>
 </environmentVariables>

这可能是问题吗?

来源:http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#environmentVariables