簡體   English   中英

如何創建使用特定 JVM 參數運行的 Spring Boot 測試

[英]How to create a Spring Boot test that runs with specific JVM arguments

我想為使用特定 JVM 參數的 Spring Boot 應用程序創建一個測試,我希望 JVM 參數僅用於此測試。

這可能嗎 ? 我的目標是為一個測試設置一個代理,所以如果有另一種方法來實現這一點,請提出建議。

我通過在類中添加一個靜態塊並設置系統屬性來解決這個問題:

static {
    System.setProperty("http.proxyHost", "myproxyserver.com");
    System.setProperty("http.proxyPort", "80");
    System.setProperty("https.proxyHost", "myproxyserver.com");
    System.setProperty("https.proxyPort", "80");
}

如果您使用的是 IntelliJ Idea,則可以從運行/調試配置 -> VM 選項傳遞參數以進行測試。 如果您使用的是 Maven,則可以在 surefire-plugin 配置中傳遞參數:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <argLine>-Xmx1G</argLine>
            </configuration>
        </plugin>

請記住,Maven 選項會覆蓋 Idea 默認值(如果設置)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM