java - 在多个环境中执行 JUnit 测试的配置

标签 java eclipse jenkins maven-3 junit4

我有一个包含 JUnit 测试的 Java 项目,需要通过 Jenkins 在不同的测试环境(Dev、Staging 等)上运行。

我目前必须在不同的环境中构建项目并将 url、用户名和密码传递给测试运行器的解决方案是在 POM 文件中为每个环境加载特定的属性文件。将通过 Maven 构建命令为每个环境设置属性文件:

mvn clean install -DappConfig=/src/test/resouces/integration.environment.properties

在 pom.xml 中:

<plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <systemPropertyVariables>
                    <appConfig>${app.config}</appConfig>
                </systemPropertyVariables>
            </configuration>
        </plugin>
    </plugins>

在 JUnit 测试运行器类中:

public class BoGeneralTest extends TestCase {

    protected WebDriver driver;
    protected BoHomePage boHomePage;
    protected static Properties systemProps;
    String url = systemProps.getProperty("Url");
    String username = systemProps.getProperty("Username");
    String password = systemProps.getProperty("Password");
    int defaultWaitTime = Integer.parseInt(systemProps.getProperty("waitTimeForElements"));
    String regUsername = RandomStringUtils.randomAlphabetic(5);

    final static String appConfigPath = System.getProperty("appConfig");

    static {
        systemProps = new Properties();
        try {

            systemProps.load(new FileReader(new File(appConfigPath)));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

此配置的问题在于,现在无法通过 Eclipse 单独运行各个测试,因为它们期望从 maven 接收 appConfig 而我收到 NullPointerException。

非常感谢任何建议。

最佳答案

运行单个测试的前提是每个测试用例都有一个运行配置,它将指定默认的执行环境。请注意,此设置必须在每个测试用例本地完成。

在 Eclipse Arguments 选项卡/VM Arguments 中,必须指定 VM 参数:

-DappConfig=src/test/resources/pp1.environment.properties

它包含具有环境登录详细信息的相应属性文件的路径。

在项目的src/test/resources源码文件夹下定义了五个属性文件:

environment1.properties
environment2.properties
environment3.properties
environment4.properties
environment5.properties

关于java - 在多个环境中执行 JUnit 测试的配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15509578/

相关文章:

Eclipse:在 checkin 之前跟踪我更改的所有文件?

java - jenkins maven build 不运行 JUnit 测试

java - 为什么接口(interface)引用显示toString()、hashCode()等Object的方法

java - AES128 解密 :javax. crypto.badpaddingexception pad block 已损坏

java - 使带注释的困惑远离领域模型

jenkins - Jenkins 中的条件作业链

jenkins - Jenkins 中的配置全局安全性缺少 JNLP 端口

java - maven可以同时使用本地和远程仓库吗?

java - 如何找回eclipse中删除的项目

eclipse - 异常加载 EMF 文件