配置 IntelliJ 以便它需要一个特定的配置文件来进行 运行 测试?

Configuring IntelliJ so it takes a specific configuration file to run tests?

我正在使用 Play Framework 2.3 和 IntelliJ IDEA 14。我在我的应用程序中使用了 Mailer 插件。在将此行添加到 build.sbt 后,我在 SBT 控制台中 运行 test 命令时编写了一些功能测试,这些测试完美运行:

javaOptions in Test += "-Dconfig.file=conf/application.test.conf"

并将此添加到文件 conf/application.test.conf:

smtp.mock=yes

不幸的是,当我 运行 直接从 IntelliJ 进行测试时,我得到了这个错误:

java.lang.RuntimeException: smtp.host needs to be set in application.conf in order to use this plugin (or set smtp.mock to true)

我尝试使用 VM 参数启动这些测试 -Dconfig.file=conf/application.test.conf,但没有成功。

以下是我尝试执行的两个测试示例:

@Test
public void testWithServer() {
    running(testServer(3333), () -> {
        assertThat(WS.url("http://localhost:3333").get().get(1000).getStatus()).isEqualTo(OK);
    });
}

@Test
public void testWithBrowser() {
    running(testServer(3333), HTMLUNIT, browser -> {
        browser.goTo("http://localhost:3333");
        assertThat(browser.$("title").getText()).isEqualTo("Welcome");
    });
}

谁能帮我解决这个问题?

谢谢!

除非我弄错了,否则 config.file 设置和 conf/application.test.conf 文件被 SBT 使用。因此,IntelliJ IDEA 在 运行 测试时不会加载它及其包含的设置,即使您通过 VM 选项文本字段指定 config.file 设置也是如此。相反,您必须将 -Dsmtp.mock=yes 设置(application.test.conf 文件中的任何其他设置)放在 VM 选项文本字段中。

如果可行,您可以将参数添加到 Run/Debug 配置对话框中默认组下的 JUnit 设置的 VM 选项文本字段,这样您创建的任何新测试都会预设.

在您的 运行 配置中,select JUnit,然后单击扳手以编辑默认配置。然后,在 VM 选项中,添加 -Dconfig.file=/absolute/path/to/application.test.conf.