可以在Java应用程序中检测系统代理设置,但不能在JUnit中检测

Ahm*_*our 16 java eclipse proxy junit system-properties

  • Windows 7的
  • Java 1.8.0_45
  • Eclipse火星

如果您将系统代理设置为HTTP,则HTTP仅当从mainjava应用程序的方法运行时,才会打印以下内容.

但是,如果从JUnit 4测试(在eclipse中)调用它,它总是打印DIRECT.

还注意到-Djava.net.useSystemProxies=true在eclipse 中定义:运行配置 - >参数 - > VM参数.测试只是挂起.

知道发生了什么事吗?

非常感谢,

public void printSystemProxy() {
    System.setProperty("java.net.useSystemProxies", "true");
    try {
        final List<Proxy> list = ProxySelector.getDefault().select(new URI("http://foo/bar"));
        for (final Proxy proxy : list) {
            System.out.println(proxy.type());
        }
    } 
    catch (final URISyntaxException e) {
        throw new IllegalStateException(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

尝试使用以下VM参数进行TestRunner配置(右键单击/ RunAs ...):

-Dhttp.proxyHost=<YOUR_PROXY>
-Dhttp.proxyPort=<YOUR_PORT>
-Dhttp.nonProxyHosts=<IF YOU NEED THIS (pipe as separator)>
-Dhttp.proxyUser=<YOUR_NAME>
-Dhttp.proxyPassword=<YOUR_PASWORD>
Run Code Online (Sandbox Code Playgroud)