在使用hjwebunit测试https的请求时,遇到测试环境的证书不能通过,所以会出现下面的错误
java.lang.RuntimeException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at net.sourceforge.jwebunit.htmlunit.HtmlUnitTestingEngineImpl.gotoPage(HtmlUnitTestingEngineImpl.java:243)
at net.sourceforge.jwebunit.junit.WebTester.gotoPage(WebTester.java:2928)
at net.sourceforge.jwebunit.junit.WebTestCase.gotoPage(WebTestCase.java:2215)
at com.taobao.login.test.web.LogoutTest.test_logout_已登录用户成功登出(LogoutTest.java:89)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at net.sourceforge.jwebunit.junit.WebTestCase.runBare(WebTestCase.java:79)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
为了解决这个问题,我们可以采用以下方式:
使用IE浏览器打开需要测试的https站点, 保存当前的证书到本地
1. 点击 证书错误
2. 查看证书
3. 保存到其它地方
4. 不停的点下一步就可以了
5. 通过java的keytool 工具我们可查看我们保存的证书
keytool -printcert -file yourCER.cer
6. 导入证书到keystore
keytool -import -v -file dev.cer -storepass secret -keystore dev.keystore -alias dev
wner: CN=dvlp.iteye.nl, O=IT-eye, L=Nieuwegein, ST=Utrecht, C=NL
Issuer: O=IT-eye, C=NL
Serial number: 6
Valid from: Mon Mar 14 15:06:35 CET 2005 until: Sat Mar 13 15:06:35 CET 2010
Certificate fingerprints:
MD5: 7B:26:F0:67:48:4C:1C:35:52:C4:BC:32:50:72:49:CE
SHA1: 94:44:33:18:59:66:BB:71:9F:5B:7C:FE:C3:A6:A8:04:2F:9B:DB:1D
7. 使用
public void test_yourMethod(){
System.setProperty("https.proxyHost", "your host ip");
System.setProperty("https.proxyPort", "your host port");
System.setProperty("https.proxySet", "true");
System.setProperty("https.nonProxyHosts", "your web server");
System.setProperty("javax.net.ssl.trustStore","d:\\jwebunit\\dev.keystore");
System.setProperty("javax.net.ssl.trustStorePassword", "secret");
//specify base url of the web application
getTestContext().setBaseUrl("https://mywebserver/app1/");
beginAt("/");
}