Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Testing network code is awkward. Good unit test suites run quickly so that developers can run the tests after every compile. Test suites must also run reliably such that they consistently catch any errors in the code. However, networked code (for example, code that reads from a URL) proves difficult to test reliably and quickly. Moreover, if the test suite itself makes network calls, the tests will run slowly and unreliably since they now depend on a network and other servers.
Consider a program that downloads, formats, and displays XML data from the Web. A naive test suite for this program would require a running Web server from which to fetch the XML data. But many parts of the program—the XML parser, the formatter, the displayer—could be tested on their own without relying on a network. With that example in mind, in this article I demonstrate two techniques for testing network-related code that avoid using the network when the tests run. I provide in-text code samples, but you can follow along by downloading the complete software.
I begin by describing PrintRSS, a simple network-enabled demonstration program, then discuss how to design the PrintRSS program
for easy testing with simple
Writer objects rather than network connections. I finish with a library that allows programmers to synthesize special
testurl: URLs that stand in for normal
http: URLs, thereby bypassing the network. Note: All tests use the JUnit test framework's
PrintRSS, a simple program that reads data from a URL and processes it, serves as a good network code testing demonstration. PrintRSS reads data in the RSS (RDF Site Summary) format, a simple XML data format used for syndicating news feeds. For the this article's purposes, the significant RSS structure is:
<rss><channel> <title>Channel Title</title> <item><title>Item 1</title></item> <item><title>Item 2</title></item> ... </channel></rss>
PrintRSS downloads an RSS document from a URL, formats the contents, then prints the titles to
System.out in an easy-to-read display:
Channel Title Item 1 Item 2
PrintRSS performs four major operations:
The PrintRSS program encapsulates all four functions in a single method,
printURL(URL). Testing this method, however, proves difficult for two reasons. First, the code relies on data loaded from the URL; if the
URL is an
http: URL, that involves using the network. And the code has its behavior buried in the side effect of writing to
System.out. Considering those problems, how can you design PrintRSS for better testing?
Because most of PrintRSS's logic simply parses and formats the XML code, rather than connecting to the network, you can factor the code so you can independently test the data logic. While refactoring the code may seem daunting, such efforts usually result in better code—both because the code is tested and because the design proves more modular.