manager Editor: Donald J. Reifer (cid:2) Reifer Consultants (cid:2) [email protected] A Little Bit of Knowledge Is a Dangerous Thing Donald J. Reifer A fter studying knowledge management loss responsibility in big organizations. It’s a in recent years, wondering if it could good thing I stayed open minded; most peo- help me manage software projects ple I contacted in organizations that had better, I’ve concluded that indeed it mounted knowledge management initiatives can. To illustrate this point, I’ve used were ecstatic about the results. Table 1 to list potential knowledge Here are several bits of what I heard: management benefits and drawbacks orga- nized around the classical functions software (cid:2) “We made a project knowledge reposi- managers perform. tory available to all players on our in- Most articles on this topic fo- tranet. Once we organized it into collec- cus on strategic, not tactical goals, tions, useful information was available describing potential benefits at the immediately—right when we needed it. 40,000-foot level. But to deter- Collaboration was fast and easy. Setting mine where you and I as software up and maintaining the collections using managers can use this technology taxonomies that the corporation created to deliver products on time and was relatively painless.” within budget, the focus must be (cid:2) “We had a lot of data that we needed to closer to the 100-foot level. keep track of from old projects. Using ex- You’ll note that the table lists a isting knowledge management tools, we “facilitating” function. That’s be- could mine the data quickly by discover- cause project managers need to bring in the ing patterns and relationships that weren’t right people at the right times to get the job immediately apparent.” done. Facilitation in this sense is high on any (cid:2) “My firm created a knowledge base of in- good manager’s priority list. For example, as formation highlighting critical success managers, you and I would need to coordi- factors for project managers. When I first nate with our legal departments to have at- viewed them, I said ‘so what?’ But when torneys available to review each subcontract. I played with the system, I found it intu- Now, let’s see how knowledge manage- itive and useful. I could take messy data ment can make your life easier, too. from my project and make sense of it once I played it through the system. I Knowledge management—hype or could also reason with the system based reality? on heuristics derived from relationships.” After I constructed this table, I was skep- tical: with these drawbacks, do managers Not all the comments were this good. I also ever take advantage of this technology? So, heard the following: I talked to members of my peer network, mostly software managers with profit-and- (cid:2) “Here we go again. Upper management 14 IEEE SOFTWARE May/June 2002 0740-7459/02/$17.00 © 2002 IEEE MANAGER must have taken a semi- that a little bit of knowledge nar. Another initiative— about knowledge manage- more time wasted at the ment can be dangerous. I’m worker level.” not too bashful to seek help (cid:2) “While it sounds good, I from those who have tackled am too busy to look at transfer of this technology, knowledge management. building on their shoulders I’ll play the wait-and-see rather than repeating their game and hope it blows mistakes. over soon.” Y ou won’t get me to say that You can imagine some of knowledge management is the rest. Give a manager a a cure-all. But I will recom- chance to purge with a peer mend that you consider using and that’s exactly what hap- it—especially if your firm has pens. But, the comments made invested in it. me think twice about how I All too often, I’ve seen could use this technology to project after project get into improve my ability to deliver. trouble because of miscom- My research into the use of munication. People are just knowledge management had too busy to tell each other panned out. Instead of joining what they’re doing. Using the naysayers, now I can think knowledge management, you of a dozen ways the tech- can make this information nology can improve my per- readily available, especially if formance. your firm helps you put the systems and procedures into Making it work developed the processes project man- place to overcome the challenges as- I’m also convinced that I’ll need agers need to structure the knowl- sociated with technology transfer. help. Not well versed in knowledge edge base of information serving as Keep an open mind, and check out management, my staff had no experi- their initiative’s foundation. These this technology. You might like it. ence with the tools and techniques. experts also developed the training My contacts at large companies were and incentives needed to put the Don Reiferis president of Reifer Consultants and visiting lucky. Their corporate initiatives technology into practice. associate of the Center for Software Engineering at the Univer- brought in experienced people who I am also skilled enough to know sity of Southern California. Contact him at [email protected]. Table 1 Project manager’s assessment of knowledge management technology Management functions Potential upsides Possible limitations Project planning Captures organizational knowledge and makes lessons that are Hard to capture lessons learned. often learned the hard way available for use in an organized and Few incentives to use knowledge, especially when under pressure systematic manner. to get plans finalized. Organizing Determines patterns of organization that work best based on Your customer might require an organization that doesn’t adhere domain knowledge. to preferred patterns. Staffing Makes it easy to identify people with the skills and knowledge An individual’s ability to fit into a team is something that only the required to fill open slots in the organizational structure. people he or she will work with can fully evaluate. Directing Makes it easy to deduce leadership and teamwork abilities. Leadership and teamwork are qualities that need to be developed, Identifies and streamlines optimal communication patterns not inferred. in the organization. While communications can be streamlined, breakdowns often Exploits lessons learned relative to past experience. occur due to people issues that must be anticipated to address. Controlling Creates benchmarks to compare performance against, both Benchmarks might be misleading because they might be based corporate and competitive. onnorms instead of specifics. Facilitating Identifies linkages between work processes and highlights Good project managers can facilitate problem solving because techniques that improve efficiency and effectiveness. they anticipate problems and bring in experts to work on them. May/June 2002 IEEE SOFTWARE 15 requirements Editor: Suzanne Robertson (cid:2) The Atlantic Systems Guild (cid:2) [email protected] Requirements and Psychology Chris Rupp “Well, I thought you meant...” “No, that’s not what I said...” sounds familiar to any require- ments engineer. Chris Rupp decided to borrow some ideas from the discipline of psychother- apy to help pin down what people really mean. Here’s a good example of how we can use well-developed ideas from other disciplines to help improve our own. —Suzanne Robertson F ulfilling your customers’ interests de- (cid:2) perception of facts (reality) termines your market success, but (cid:2) linguistic representation of one of these how do you find these requirements perceptions (personal reality) effectively and efficiently? As simple as this question sounds, answering it in The goal of semantic language analysis is to daily practice is difficult. Often, stake- uncover formulations borne of subjective holders are interviewed about experience and replace them with clear, ob- their requirements or asked to jective formulations. write them down, but this ap- Since 1995, our company, Sophist GmbH, proach rarely uncovers the real has applied language models such as the requirements that reflect a cus- Requirements Engineering (RE)-Metamodel tomer’s true interests or needs. to areas of computer science. The result has We need a way of getting infor- been simple, easy-to-use rules for prose re- mation about the customers’ quirements in project development. Using core desires—conscious, uncon- these adaptable rules, we can systematically scious, even subconscious. The review requirements formulated in natural hottest sellers are products that language so that errors or ambiguities never fulfill these desires. see the light of day. Linguistics The RE-Metamodel Communication between people is never Figure 1 shows the RE-Metamodel, sum- easy, especially when it’s about requirements marizing the most frequently observed lan- for complex systems or products. To help guage phenomena linguistically. Each of overcome communication problems, I rec- these phenomena indicates linguistic defects. ommend a method from the area of neu- I will explain an example of each type of de- rolinguistic programming (NLP), which be- fect and how you can recognize them in longs to the field of psychotherapy. (For your own work. further research into this topic, please see R. Bandler and J. Grinder, The Structure of Deletion Magic, Science and Behavior Books, 1990.) We all use the process of deletion to re- This conversational method tries to decode duce the world to dimensions that we can what a person means when speaking. handle. In requirements engineering, how- According to NLP, mistakes in statements ever, we must know exactly what informa- can happen in two places: tion we’ve lost, or we run the risk of a mis- 16 IEEE SOFTWARE May/June 2002 0740-7459/02/$17.00 © 2002 IEEE REQUIREMENTS Deletion ➯ Presuppositions ➯ Incomplete comparatives and superlatives Generalization ➯ Modal operators of possibility ➯ Universal quantifiers ➯ Modal operators of necessity ➯ Incompletely specified conditions Distortion ➯ Incompletely defined process words ➯ Nouns without referential indices ➯ Nominalizations Figure 1. The RE-Metamodel, summarizing the most frequently observed linguistic defects. understanding. One way to find basis of the keyword “each,” a ques- is clearly defined. This definition deleted information is to examine a tion immediately arises: Are there should not leave any margin for in- sentence’s process words, or verbs. one or more special cases in which terpretation of the process and To be complete, a process word the time stamp is not required? should clarify its progression as well usually requires an argument or a You probably won’t find many as all input and output parameters. noun phrase. Consider the following: universal quantifiers when searching The goal is not necessarily to avoid “The system shall report data loss.” your text, which is not necessarily a nominalizations but to use them only The process word “report” is com- good sign. Inspect sentences that if the process lying behind them is pletely defined only if the following don’t contain explicit statements clear. Nominalizations often appear questions are answered: Who reports about the number of objects for in domains with an extensive techni- what to whom? What is reported? which the specified reaction occurs. cal language, so check your field’s When? How? They often contain an implicit as- terminology. Certainly you will iden- For each requirement, you must be sumption that the specified reaction tify most specialist terms as nominal- sure that the process word in it is a sat- is valid for all relevant objects. izations that hide a large amount of isfactory and clear-without-ambiguity Sometimes knowing how much specialized knowledge. definition of the actual process. Identify- time and effort to spend on special ing and roughly specifying the processes cases and exceptions is hard. It all Applying the metamodel that your system should carry out will comes back to assessing the risk of not Many companies have success- guide you in asking questions that clar- asking the questions. In cases where fully used the RE-Metamodel in var- ify meaning. If dynamics and complex safety of life and limb is involved, you ious project areas: Deutsche processing rules play a deciding role in can’t afford to miss exceptions. Flugsicherung, Swisscontrol, and Eu- your system, pay special attention to rocontrol (air traffic control sys- process words. They can lead you to a Distortion tems); Deutsche Post (logistic sys- clear definition of when and in what The problem of distortion appears tems); Kreditwerk and Bausparkasse condition your system performs its almost exclusively in the form of Schwäbisch Hall (societal systems); processes. nominalization, which occurs when a and Stoll GmbH (knitting machines). process is reformulated into an event. In all these different areas, the RE- Generalizations A nominalization can change a Metamodel helped systematize the In the generalization process, re- statement’s meaning and cause im- engineering of stakeholder needs. quirements seem to apply to the sys- portant information regarding a Stakeholders used additional tem- tem as a whole, but in fact they usu- process to be lost. Linguistically, plates to formulate requirements, ally apply to just a small piece of it. nominalization is a process word which in turn clearly defined how to Typical indicators of generalization (verb or predicate) molded into an finalize them (see www.sophist.de for are universal quantifiers: parts of event word (noun or argument). more information). Clear precondi- statements broadly applied to all of Consider the following: “After a sys- tions on how to elicit and accurately an occurrence’s incidences. tem breakdown, a restart shall auto- document requirements is especially Representatives of linguistic quan- matically be initiated.” The processes helpful in cases where they are not tifiers include concepts such as behind the nouns “system,” “break- noted in the stakeholders’ primary “never,” “always,” “no,” “every,” down,” and “restart” actually con- language. Most analysts and stake- and “all.” The danger in using them sist of a system breaking down and a holders love to receive precise work- is that the specified behavior does system restarting. But, how is the ing instructions because specifying a not always apply to all the referenced restart being performed? Who initial- new system can be a complex and objects in a group or set. Consider izes it? What ends it? nonintuitive process. the following: “Each signal shall be Using nominalized terms for a You can apply the RE-Metamodel labeled with a time stamp.” On the complex process is fine—if the process in three ways: May/June 2002 IEEE SOFTWARE 17 REQUIREMENTS (cid:2) Dialogue. The analyst immedi- your thinking enough for you to ex- Good systems analysts have a ately checks each stakeholder amine and scrutinize your customers’ complete suitcase of elicitation tech- statement for linguistic effects and statements in real time. niques from various sectors. This studies missing facts that appear suitcase should contain therapeutic- to be important. Thus, interviews What makes a good analyst? sector approaches (NLP, family ther- quickly get down to the nitty Knowing that perfect communica- apy) as well as creativity techniques gritty, efficiently eliciting the re- tions are nearly impossible relieves (brainstorming), behavioral science quired knowledge and promptly you of trying to achieve a perfection methods (observation techniques), revealing knowledge gaps. This you never could reach. Managing the and computer science approaches technique requires an experienced incompleteness of communication is (use-case-driven modeling, prototyp- analyst. The interviewee should core to mastering the RE process. ing techniques). not notice that the analyst is sys- The point of developing approaches A good analyst’s working method tematically inspecting all state- such as the RE-Metamodel is to help is never really predictable. He or she ments for linguistic effects. To do systems analysts be more effective in starts the conversation with a stake- this, the analyst must be experi- doing a complex job. holder and decides, based on the sit- enced enough to identify and ask Good analysts mediate between uation, which technique to use. Fac- essential questions. worlds. They investigate the stake- tors influencing that decision include (cid:2) Inspection of written require- holders’ needs while discovering, for- ments. Using linguistic rules, the mulating, and inventing requirements. (cid:2) Power and trust relationships analyst inspects already existing They then translate and transmit this within the team requirements for omissions, lack information to the world of system (cid:2) Whether the demanded informa- of clarity, and so forth according development and informatics. tion is lodged in the conscious, un- to the metamodel’s rules. Analysts do much more than conscious, or subconscious (cid:2) Combination. Of course, you can translate stakeholders’ wishes into re- (cid:2) Whether the stakeholder is good combine both approaches. quirements, however. Excellent com- at explaining knowledge (prefers municative and therapeutic skills are to work with concrete examples Ultimately, the analyst decides expected. In cases of diverging opin- or abstract concepts) how to use the RE-Metamodel, ions between stakeholders, analysts (cid:2) Whether the system is concerned adapting it to each particular situa- must work out a common essence with dynamic–active procedures tion on a case-by-case basis. With and develop acceptable solutions for or static information some concessions, the RE-Metamodel all people involved. They must over- can help represent a complete, fault- come the stakeholders’ hindrances The analyst should develop a flair less set of requirements using the in- and fears; every new system carries for using the right technique in the right formal notation of natural language. the potential to create fear by scruti- situation. A mixture of many different Thus, the door is opened for the ana- nizing previous positions and habits. techniques can bring revolutionary suc- lyst and stakeholder to work to- Analysts should have experience in cess (see www.systemsguild.com). gether to review and criticize the group dynamics, conflict manage- problem’s working description. ment, and team management apart T Usually, the NLP approach is im- from sound IT and domain know-how. o be a successful requirements en- parted at a two- or three-day training gineer, be prepared to always be in course. Afterward, you can success- a learning situation, accept fully apply it immediately because change, and never do things by rote. you don’t have to learn a new way of The variety of techniques, stakehold- thinking, you just have to explore Analysts do much more ers, and domains make the job of RE your knowledge of language. You’re an increasingly challenging and en- than translate learning a model to make that knowl- joyable one. edge more accessible and some refine- stakeholders’ wishes ments to help you apply it to RE. into requirements, For the first application of the however. Excellent method after training, we recom- mend using it to analyze already ex- communicative and isting documents. On the basis of ex- therapeutic skills isting requirements, you can test each statement for linguistic effects and are expected. Chris Ruppis the managing director of Sophist GmbH and scrutinize it before interviewing Sophist Technologies GmbH. Contact her at Sophist Gesellschaft für innovative Softwareentwicklung mbH, Vordere Cramergasse stakeholders. A few days of practice 11-13, 90478 Nuremberg, Germany; [email protected]; will integrate the RE-Metamodel into www.sophist.de. 18 IEEE SOFTWARE May/June 2002 design Editor: Martin Fowler (cid:2) ThoughtWorks (cid:2) [email protected] Yet Another Optimization Article Martin Fowler T his is a troubling column to write. I Steps for optimization hadn’t planned to write on optimiza- Optimizing an existing program follows tion, because what I have to say has a specific set of steps. First, you need a pro- already been said numerous times. filer—a program that can analyze how Yet, when I give talks, I find there’s much time your program spends in its var- still a surprising number of people ious parts. Software performance has an who don’t know, or at least don’t follow, the 80/20 rule: 80 percent of the program’s advice I’m about to give. So, here goes. time is spent on about 20 percent of the (Many of you have probably seen this ad- code. Trying to optimize performance in vice before—my thought to you the 80 percent of code is futile, so the first is to ponder why I need to say order of business is to find that 20 percent this again.) of code. Trying to deduce where the pro- First, performance matters. Al- gram will spend its time is also futile. I though relying on Moore’s law to know plenty of experienced programmers get us out of slow programs has who always get this wrong. You have to its merits, I find it increasingly an- use a profiler. noying when I get a new version To give the profiler something to chew on, of a program and must upgrade perform some kind of automated run that my hardware for it to work ac- reasonably simulates the program under its ceptably. The question is, “How usual conditions. An automated test suite is a do we achieve a fast program?” good starting point, but make sure you simu- For many programmers, performance is late the actual conditions. My colleague Dave something you pay continuous attention to Rice has a rule: “Never optimize a multiuser as you program. Every time you write a system with single-user tests.” Experience has fragment of code, you consider the perfor- taught us that a multiuser database system mance implications and code the program to has very different bottlenecks than a single maximize performance. This is an obvious user system—often focused around transac- technique—pity it works so badly. tion interactions. The wrong set of tests can Performance is not something you can easily lead you to the wrong 20 percent of work on in this way. It involves specific dis- code. cipline. Some performance work comes Once you’ve found your bottlenecks, from architectural decisions, some from a you have two choices: speed up the slow more tactical optimization activity. But things or do the slow things less often. In ei- what both share is the fact that it is diffi- ther case, you must change the software. cult to make decisions about performance This is where having a well-designed piece from just looking at the design. Rather, you of software really helps. It’s much easier to have to actually run the code and measure optimize cohesive, loosely coupled mod- performance. ules. Breaking down a system into many 20 IEEE SOFTWARE May/June 2002 0740-7459/02/$17.00 © 2002 IEEE DESIGN small pieces lets you narrow down Some exceptions Some have taken this further, com- the bottlenecks. Having a good auto- Although most performance issues ing up with performance models that mated test suite makes it easier to are best dealt with in these kinds of you can use to assess different archi- spot bugs that might slip in during optimizations, at times other forms tectural designs. Although this can be optimization. of thinking are important—for ex- handy, it’s difficult to take it too far. It’s worth knowing about various ample, during early architectural It all depends on how good the per- optimization tricks, many of which stages, when you’re making decisions formance model is, and people usu- are particular to specific languages that will be costly to reverse later. ally cannot predict the key factors, and environments. The most impor- Again, the only way to really under- even at a broad level. Again, the only tant thing to remember is that the stand these performance issues is to real arbiter is measurement. tricks are not guaranteed to work—as use measurements. In this case, you In the end, however, performance the saying goes, “Measure twice, cut build exploratory prototypes to per- is not an absolute. Getting a program once.” Unlike a tailor, however, you form crude simulations of the envi- to run faster costs money, and it’s a measure before and after you’ve ap- ronments with which you’re going to business decision whether to invest in plied the optimization. Only then do work and to get a sense of the rela- a quicker program. One value of an you know if it’s had any effect. It’s re- tive speeds. It’s tricky, of course, to explicit optimization phase is that vealing how often an optimization has get a good idea of what the actual en- the cost of getting fast performance is little—or even a negative—effect. If vironment might be, but then it’s more explicit, so businesses can trade you make an optimization and don’t likely that everything you’re working it against time to market or more fea- measure to confirm the performance with will be upgraded before you go tures. Much of the reason why I increase, all you know for certain is live anyway. Experiments are still curse at slow software is because it that you’ve made your code harder to much better than wild guesses. makes good business sense for the read. There are also some cases where builder. This double measurement is all the there are broad rules about slow more important these days. With opti- things. An example I always come mizing compilers and smart virtual across is the slowness of remote pro- machines, many of the standard opti- cedure calls. Because remote calls are mizing techniques are not just ineffec- orders of magnitude slower than in- tive but also counterintuitive. Craig process calls, it’s important to mini- Larman really brought this home mize them, which greatly affects A when he told me about some com- overall design. However, this doesn’t s I’ve said, much of this advice— ments he received after a talk at trump measuring. I once came across in particular, the advice to write a JavaOne about optimization in Java. a situation where people optimize re- good clean program first and op- One builder of an optimizing virtual mote methods only to find their bot- timize it later—is well worn. (For a machine said, in effect, “The com- tlenecks were elsewhere. However, longer description of this approach, ments about thread pools were good, minimizing remote calls has proven read Chapters 28 and 29 of Steve but you shouldn’t use object pools be- to be a solid rule of thumb. McConnell’s Code Complete, Mi- cause they will slow down our VM.” crosoft Press, 1993.) Good quotes Then another VM builder said, “The about the perils of premature opti- comments about object pools were mization have been around for over good, but you shouldn’t use thread 20 years. The pity is that some peo- pools because they slow down our ple still object when I call the same VM.” Not only does this reinforce the query method twice in a routine. For need to measure with every optimiza- If you make an everyone who finds nothing new in tion change, it also suggests that you this column, there exists another optimization and should log every change made for op- challenge—how to make it so there is timization (a comment tag in the don’t measure to no need to rewrite it in 10 years. source code is a good option) and confirm the performance retest your optimizations after up- grading your compiler or VM. The increase, all you know optimization you did six months ago for certain is that could be your bottleneck today. you’ve made your code All of this reinforces the key rule that first you need to make you pro- harder to read. gram clear, well factored, and nicely Martin Fowleris the chief scientist for ThoughtWorks, an modular. Only when you’ve done Internet systems delivery and consulting company. Contact him that should you optimize. at [email protected]. May/June 2002 IEEE SOFTWARE 21 software construction Editors: Andy Hunt and Dave Thomas (cid:2) The Pragmatic Programmers [email protected] (cid:2) [email protected] Mock Objects Dave Thomas and Andy Hunt Yet sit and see; Minding true things by what their mockeries be. —Shakespeare, Henry V O ne thing that makes unit-testing code drop off, and we all know where that leads. so hard is the way the real world Fortunately there’s a testing pattern that keeps intruding. If all we had to do can help. Using mock objects, you can test was code up tests for methods that code in splendid isolation, simulating all sort arrays or generate Fibonacci se- those messy real-world things that would ries, life would be easy. But in the real otherwise make automated testing impossi- world we have to test code that uses data- ble. And, as with many other testing prac- bases, communications devices, user inter- tices, the discipline of using mock objects faces, and external applications. We might can improve your code’s structure. An example: Testing a servlet Servlets are chunks of code that a Web server manages. Requests to certain URLs are forwarded to a servlet container (or man- ager) such as Jakarta Tomcat (http://jakarta. apache.org/tomcat), which in turn invokes the servlet code. The servlet then builds a re- sponse that it sends back to the requesting browser. From the end user’s perspective, it’s just like accessing any other page. Figure 1 shows part of the source of a have to interface to devices that aren’t yet trivial servlet that converts temperatures available or simulate network errors that are from Fahrenheit to Celsius. Let’s quickly impossible to generate locally. This all con- step through its operation. When the servlet spires to stop our unit tests from being neat, container receives the request, it automati- self-contained (and orthogonal) chunks of cally invokes the servlet method doGet(), code. Instead, if we’re not careful, we find passing in two parameters, a request and a ourselves writing tests that end up initializ- response. (These are important for our test- ing nearly every system component just to ing later). The request parameter contains give the tests enough context to run. Not information about the request; the servlet’s only is this time consuming, it also intro- job is to fill in the response. The servlet’s duces a ridiculous amount of coupling into body gets the contents of the field “Fahren- the testing process: someone changes an in- heit” from the request, converts it to Cel- terface or a database table, and suddenly the sius, and writes the results back to the user. setup code for your poor little unit test dies The writing is done via a PrintWriter object, mysteriously. Even the best-intentioned de- which a factory method in the response ob- velopers become discouraged after this hap- ject provides. If an error occurs converting pens a few times. Eventually, testing starts to the number (perhaps the user typed “boo!” 22 IEEE SOFTWARE May/June 2002 0740-7459/02/$17.00 © 2002 IEEE SOFTWARE CONSTRUCTION into the form’stemperature field), we 1 public void doGet(HttpServletRequest req, catch the exception and report the er- 2 HttpServletResponse res) ror in the response. 3 throws ServletException, IOException Having written this code (or be- 4 { fore writing it, for those in the Ex- 5 String str_f = req.getParameter(“Fahrenheit”); 6 treme Programming tribe), we’ll want 7 res.setContentType(“text/html”); a unit test to verify it. This is where 8 PrintWriter out = res.getWriter(); things start looking difficult. This 9 snippet of code runs in a fairly com- 10 try { plex environment (a Web server and a 11 int temp_f = Integer.parseInt(str_f); servlet container), and it requires a 12 double temp_c = (temp_f – 32) * 5.0 / 9.0; user sitting at a browser to interact 13 out.println(“Fahrenheit: “ + temp_f + “, Celsius: “ + temp_c); with it. This is hardly the basis of a 14 } good automated unit test. 15 catch (NumberFormatException e) { But let’s look at our servlet code 16 out.println(“Invalid temperature: “ + str_f); again. Its interface is pretty simple: as 17 } we mentioned before, it receives two 18 } parameters, a request and a response. The request object must be able to Figure 1. A trivial servlet that converts temperatures from provide a reasonable string when its Fahrenheit to Celsius. getParameter() method is called, and the response object must support (cid:2) The real object has nondeterminis- We can use mock objects in two dis- setContentType()and getWriter(). tic behavior. tinct ways. First, we can use them to It’s starting to look as if we might be (cid:2) The real object is difficult to set up. set up an environment in which our able to write some stubs: objects that (cid:2) The real object has behavior that test code runs: we can initialize values pretend to be real request and response is hard to trigger (for example, a in the objects that the method under objects but that contain just enough network error). test uses. Figure 2 shows a typical set logic to let us run our code. In principle (cid:2) The real object is slow. of tests using the JUnit testing frame- this is easy: both HttpServletRequest (cid:2) The real object has (or is) a user work, which is available at www.junit. and HttpServletResponse are inter- interface. org. We use a MockHttpServletRe- faces, so all we have to do is whip up a (cid:2) The test needs to ask the real ob- quest object to set up the context in couple of classes that implement the in- ject about howit was used (for ex- which to run the test. On line six of the terfaces and we’re set. Unfortunately, ample, a test might need to check code, we set the parameter “Fahren- when we look at the interface, we dis- to see that a callback function was heit” to the value “boo!” in the re- cover that we’ll need to implement actually called). quest object. This is equivalent to the dozens of methods just to get the thing (cid:2) The real object does not yet exist. user entering “boo!” in the corre- to compile. Fortunately, other folks sponding form field; our mock object have already done the work for us. Mackinnon, Freeman, and Craig eliminates the need for human input also developed the code for a mock ob- when the test runs. Mock objects ject framework for Java programmers Mock objects can also verify that Tim Mackinnon, Steve Freeman, (available at www.mockobjects.com). actions were taken. On line seven of and Philip Craig introduced the con- Let’s use that code to test our servlet. Figure 2, we tell the response object cept of mock objects in their paper The good news is that in addition that we expect the method under test “Endo-Testing: Unit Testing with to the underlying framework code, the to set the response’s content type to Mock Objects” (www.cs.ualberta.ca/ mockobjects package comes with a text/html. Then, on lines 9 and 22, ~hoover/cmput401/XP-Notes/xp-conf/ number of mocked-up application-level after the method under test has run, Papers/4_4_MacKinnon.pdf), which objects. You’ll find mock output objects we tell the response object to verify they presented at XP2000. Their idea (OutputStream, PrintStream, and that this happened. Here, the mock is a natural extension of the ad hoc PrintWriter), objects that mock the object eliminates the need for a hu- stubbing that testers have been doing java.sql library, and classes for fak- man to check the result visually. This all along. The difference is that they ing out a servlet environment. In par- example shows a pretty trivial verifi- describe a framework to make writ- ticular, the package provides mocked- cation: in reality, mock objects can ing mock objects and incorporating up versions of HttpServletRequest verify that fairly complex sequences them into unit testing easier. and HttpServletResponse, which by of actions have been performed. Their paper lists seven good rea- an incredible coincidence are the types Mock objects can also record the sons to use a mock object (para- of the parameters of the method we data that was given to them. In our phrased slightly here): want to test. case, the response object receives the May/June 2002 IEEE SOFTWARE 23 SOFTWARE CONSTRUCTION hoc basis, coding up stubs when we 1 public void test_bad_parameter() throws Exception { needed them. However, we 2 TemperatureServlet s = new TemperatureServlet(); personally have recently started 3 MockHttpServletRequest request = benefiting from adopting a more new MockHttpServletRequest(); systematic approach to creating 4 MockHttpServletResponse response = new MockHttpServletResponse(); mock objects. Even things as simple 5 as consistent naming schemes have 6 request.setupAddParameter(“Fahrenheit”, “boo!”); helped make our tests more readable 7 response.setExpectedContentType(“text/html”); and the mock objects themselves more 8 s.doGet(request, response); portable from project to project. 9 response.verify(); There are several mock object frame- 10 assertEquals(“Invalid temperature: boo!\r\n”, works to choose from. Three for Java 11 response.getOutputStreamContents()); are at www.c2.com/cgi/wiki?MockOb- 12 } ject, and a fine implementation for Ruby 13 is at www.b13media.com/dev/ruby/ 14 public void test_boil() throws Exception { mock.html. If the thought of writing 15 TemperatureServlet s = new TemperatureServlet(); all the mock object classes you might 16 MockHttpServletRequest request = need is intimidating, look at Easy- new MockHttpServletRequest(); Mock (www.easymock.org), a con- 17 MockHttpServletResponse response = venient Java API for creating mock new MockHttpServletResponse(); objects dynamically. All these imple- 18 mentations are a starting point; you’ll 19 request.setupAddParameter(“Fahrenheit”, “212”); 20 response.setExpectedContentType(“text/html”); probably need to add new mock object 21 s.doGet(request, response); implementations to stub out real ob- 22 response.verify(); jects in your environment. 23 assertEquals(“Fahrenheit: 212, Celsius: 100.0\r\n”, There are also alternatives to 24 response.getOutputStreamContents()); mock objects in the servlet environ- 25 } ment. In particular, the Jakarta Cac- tus system (http://jakarta.apache.org/ Figure 2. Mock objects in action—a typical set of tests using the cactus) is a heavier-weight frame- JUnit testing framework. work for testing server-side compo- nents. Compared to the mock-objects text that our servlet wants to dis- Mock objects approach, Cactus runs your tests in play on the browser. We can query It’s likely that we’ve all been using the actual target environment and this value (lines 10 and 23) to mock objects for years without tends to produce less fine-grained check that we’re returning the text knowing it. However, it’s also likely tests. Depending on your needs, this we were expecting. that we’ve used them only on an ad might or might not be a good thing. A funny thing happens when you Practical Experience with Mock Objects start using mock objects. As with Nat Pryce other low-level testing practices, you might find that your code be- My experience is that using mock objects and a test-first methodology forces comes not only better tested but also you to think about design differently from traditional object-oriented design tech- better designed and easier to under- niques. First, you end up with many small, decoupled classes that are used stand (Nat Pryce discusses this in the through composition, rather than inheritance. These classes implement interfaces sidebar). Mock objects won’t solve more than they inherit state and behavior. Second, you think about object inter- all your development problems, but faces in terms of the services that an object both provides and requiresfrom its they are exceptionally sharp tools to environment, making an object’s requirements explicit. This is different from have in your toolbox. traditional OO design methods that concentrate only on an object’s provided services and try to hide an object’s requirements through encapsulation of pri- vate-member variables. Third, you end up thinking more explicitly in terms of Dave Thomasand Andy Huntare partners in The Pragmatic Programmers, LLC. They feel that software consultants interobject protocols. Those protocols are often defined using interface definitions who can’t program shouldn’t be consulting, so they keep current and tested using mock objects before a concrete implementation is produced. by developing complex software systems for their clients. They also offer training in modern development techniques to program- Nat Pryceis the technical director at B13media Ltd. Contact him at [email protected]; www.b13media.com. mers and their management. They are coauthors of The Pragmatic Programmerand Programming Ruby, both from Addison-Wesley. Contact them via www.pragmaticprogrammer.com. 24 IEEE SOFTWARE May/June 2002