如何在 IntelliJ IDEA 中为 JUnit 测试配置全局代理
如果你希望 所有 JUnit 测试 运行时自动走代理(而不需要每个测试类单独配置),可以通过以下方法实现:
✅ 方法 1:修改 JUnit 运行模板(推荐)
适用场景:所有 JUnit 测试(包括 @Test 方法)默认使用代理。
步骤
打开运行配置模板:
- 点击 IDEA 顶部菜单栏的
Run→Edit Configurations。 - 在左侧选择
Templates→JUnit。
- 点击 IDEA 顶部菜单栏的
在
VM Options中添加代理参数:plaintext-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=7897 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=7897-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=7897 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=7897- 如果你的代理是 SOCKS5(如 Shadowsocks),改用:plaintext
-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=7897-DsocksProxyHost=127.0.0.1 -DsocksProxyPort=7897
- 如果你的代理是 SOCKS5(如 Shadowsocks),改用:
保存并应用:
- 点击
Apply→OK。
- 点击
验证:
- 新建一个 JUnit 测试类,运行测试时检查代理是否生效:java
@Test public void testProxy() { System.out.println("HTTP Proxy: " + System.getProperty("http.proxyHost")); System.out.println("HTTPS Proxy: " + System.getProperty("https.proxyHost")); }@Test public void testProxy() { System.out.println("HTTP Proxy: " + System.getProperty("http.proxyHost")); System.out.println("HTTPS Proxy: " + System.getProperty("https.proxyHost")); } - 如果输出代理 IP,说明配置成功。
- 新建一个 JUnit 测试类,运行测试时检查代理是否生效:
✅ 方法 2:通过 JAVA_TOOL_OPTIONS 环境变量(全局生效)
适用场景:希望所有 Java 程序(包括 JUnit、Maven/Gradle 测试)都走代理。
步骤
设置环境变量:
- Windows:
- 打开
系统属性→高级→环境变量。 - 在
系统变量中新建:- 变量名:
JAVA_TOOL_OPTIONS - 变量值:plaintext
-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=7897 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=7897-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=7897 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=7897
- 变量名:
- 打开
- macOS/Linux: 在
~/.bashrc或~/.zshrc中添加:bash然后运行:export JAVA_TOOL_OPTIONS="-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=7897 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=7897"export JAVA_TOOL_OPTIONS="-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=7897 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=7897"bashsource ~/.bashrcsource ~/.bashrc
- Windows:
重启 IDEA,运行 JUnit 测试验证代理是否生效。
✅ 方法 3:在 pom.xml 或 build.gradle 中配置(Maven/Gradle 项目)
适用场景:项目使用 Maven/Gradle,希望测试时自动应用代理。
Maven 配置(pom.xml)
xml
<project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=7897
-Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=7897
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project><project>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>
-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=7897
-Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=7897
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>Gradle 配置(build.gradle)
groovy
test {
systemProperties = [
"http.proxyHost": "127.0.0.1",
"http.proxyPort": "7897",
"https.proxyHost": "127.0.0.1",
"https.proxyPort": "7897"
]
}test {
systemProperties = [
"http.proxyHost": "127.0.0.1",
"http.proxyPort": "7897",
"https.proxyHost": "127.0.0.1",
"https.proxyPort": "7897"
]
}❌ 常见问题
问题 1:JUnit 测试仍不走代理
- 可能原因:
- 代理参数被其他配置覆盖。
- 代理工具未正常运行(检查
curl -x http://127.0.0.1:7897 https://www.google.com)。
- 解决方案:
- 确保代理工具(如 Clash、v2ray)已启动并监听正确端口。
- 在代码中强制设置代理(不推荐长期使用):java
@BeforeAll public static void setupProxy() { System.setProperty("http.proxyHost", "127.0.0.1"); System.setProperty("http.proxyPort", "7897"); }@BeforeAll public static void setupProxy() { System.setProperty("http.proxyHost", "127.0.0.1"); System.setProperty("http.proxyPort", "7897"); }
问题 2:HTTPS 请求失败
- 原因:Java 不信任代理工具的证书。
- 解决:
- 在代理参数中添加信任所有证书(仅测试环境):plaintext
-Djavax.net.ssl.trustStoreType=Windows-ROOT # Windows -Djavax.net.ssl.trustStore=/dev/null # macOS/Linux-Djavax.net.ssl.trustStoreType=Windows-ROOT # Windows -Djavax.net.ssl.trustStore=/dev/null # macOS/Linux
- 在代理参数中添加信任所有证书(仅测试环境):
📌 总结
| 方法 | 适用场景 | 备注 |
|---|---|---|
| 修改 JUnit 模板 | 所有 JUnit 测试默认走代理 | 推荐!简单直接 |
环境变量 JAVA_TOOL_OPTIONS | 全局生效(包括命令行) | 影响所有 Java 程序 |
| Maven/Gradle 配置 | 项目级代理设置 | 适合团队协作 |
推荐优先使用 方法 1(修改 JUnit 模板),避免影响其他 Java 程序。
FCAT