如何创建使用特定 JVM 参数运行的 Spring 启动测试
How to create a Spring Boot test that runs with specific JVM arguments
我想为我的 spring 启动应用程序创建一个测试,它使用特定的 JVM 参数,我希望 JVM 参数仅用于此测试。
这可能吗?我的目标是只为一个测试设置一个代理,所以如果有另一种方法可以实现,请提出建议。
如果您使用的是 IntelliJ Idea,则可以通过 Run/Debug 配置 -> VM 选项将参数传递给测试。
如果你使用 Maven,你可以在 surefire-plugin 配置中传递参数:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xmx1G</argLine>
</configuration>
</plugin>
请记住,Maven 选项会覆盖 Idea 默认值(如果已设置)。
我通过在 class 中添加一个静态块并设置系统属性解决了这个问题:
static {
System.setProperty("http.proxyHost", "myproxyserver.com");
System.setProperty("http.proxyPort", "80");
System.setProperty("https.proxyHost", "myproxyserver.com");
System.setProperty("https.proxyPort", "80");
}
我想为我的 spring 启动应用程序创建一个测试,它使用特定的 JVM 参数,我希望 JVM 参数仅用于此测试。
这可能吗?我的目标是只为一个测试设置一个代理,所以如果有另一种方法可以实现,请提出建议。
如果您使用的是 IntelliJ Idea,则可以通过 Run/Debug 配置 -> VM 选项将参数传递给测试。 如果你使用 Maven,你可以在 surefire-plugin 配置中传递参数:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xmx1G</argLine>
</configuration>
</plugin>
请记住,Maven 选项会覆盖 Idea 默认值(如果已设置)。
我通过在 class 中添加一个静态块并设置系统属性解决了这个问题:
static {
System.setProperty("http.proxyHost", "myproxyserver.com");
System.setProperty("http.proxyPort", "80");
System.setProperty("https.proxyHost", "myproxyserver.com");
System.setProperty("https.proxyPort", "80");
}