在使用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("/");
}