测试具有URLConnection的方法的最简单方法是什么?

时间:2016-03-03 14:03:38

标签: testing mocking spock

测试具有URLConnection的方法的最简单方法是什么?问题是,我需要从这个URLConnection获取一个InputStream。所以我不知道如何模拟这个类,因为URL是最终类。

1 个答案:

答案 0 :(得分:1)

最简单的方法是在文件上创建一个URL,这是测试的一部分:

raw = [u'\n#\n# ARIN WHOIS data and services are subject to the Terms of Use\n# available at: https://www.arin.net/whois_tou.html\n#\n# If you see inaccuracies in the results, please report at\n# https://www.arin.net/public/whoisinaccuracy/index.xhtml\n#\n\n\n#\n# Query terms are ambiguous.  The query is assumed to be:\n#     "n 132.245.55.8"\n#\n# Use "?" to get help.\n#\n\n#\n# The following results may also be obtained via:\n# https://whois.arin.net/rest/nets;q=132.245.55.8?showDetails=true&showARIN=false&showNonArinTopLevelNet=false&ext=netref2\n#\n\nNetRange:       132.245.0.0 - 132.245.255.255\nCIDR:           132.245.0.0/16\nNetName:        MICROSOFT\nNetHandle:      NET-132-245-0-0-1\nParent:         NET132 (NET-132-0-0-0-0)\nNetType:        Direct Assignment\nOriginAS:       \nOrganization:   Microsoft Corp (MSFT-Z)\nRegDate:        2011-06-22\nUpdated:        2013-08-20\nRef:            https://whois.arin.net/rest/net/NET-132-245-0-0-1\n\n\n\nOrgName:        Microsoft Corp\nOrgId:          MSFT-Z\nAddress:        One Microsoft Way\nCity:           Redmond\nStateProv:      WA\nPostalCode:     98052\nCountry:        US\nRegDate:        2011-06-22\nUpdated:        2015-10-28\nComment:        To report suspected security issues specific to \nComment:        traffic emanating from Microsoft online services, \nComment:        including the distribution of malicious content \nComment:        or other illicit or illegal material through a \nComment:        Microsoft online service, please submit reports \nComment:        to:\nComment:        * https://cert.microsoft.com.  \nComment:        \nComment:        For SPAM and other abuse issues, such as Microsoft \nComment:        Accounts, please contact:\nComment:        * abuse@microsoft.com.  \nComment:        \nComment:        To report security vulnerabilities in Microsoft \nComment:        products and services, please contact:\nComment:        * secure@microsoft.com.  \nComment:        \nComment:        For legal and law enforcement-related requests, \nComment:        please contact:\nComment:        * msndcc@microsoft.com\nComment:        \nComment:        For routing, peering or DNS issues, please \nComment:        contact:\nComment:        * IOC@microsoft.com\nRef:            https://whois.arin.net/rest/org/MSFT-Z\n\n\nOrgTechHandle: MRPD-ARIN\nOrgTechName:   Microsoft Routing, Peering, and DNS\nOrgTechPhone:  +1-425-882-8080 \nOrgTechEmail:  IOC@microsoft.com\nOrgTechRef:    https://whois.arin.net/rest/poc/MRPD-ARIN\n\nOrgAbuseHandle: MAC74-ARIN\nOrgAbuseName:   Microsoft Abuse Contact\nOrgAbusePhone:  +1-425-882-8080 \nOrgAbuseEmail:  abuse@microsoft.com\nOrgAbuseRef:    https://whois.arin.net/rest/poc/MAC74-ARIN\n\n\n#\n# ARIN WHOIS data and services are subject to the Terms of Use\n# available at: https://www.arin.net/whois_tou.html\n#\n# If you see inaccuracies in the results, please report at\n# https://www.arin.net/public/whoisinaccuracy/index.xhtml\n#\n\n']

pattern_strings = ['address:(.*?)(?=\\nphone:)', 'Address(.*?)(?=\\nRegDate:)']
pattern_string = '|'.join(pattern_strings)
address_t = re.compile(pattern_string)
address_t2 = address_t.search(raw)            
if address_t2.group(0):
    address = address_t2.group(0)
if address_t2.group(1):
    address = address_t2.group(1)