Growing Object-Oriented Software, Guided by Tests- P9
lượt xem 4
download
Growing Object-Oriented Software, Guided by Tests- P9: Test-Driven Development (TDD) hiện nay là một kỹ thuật được thành lập để cung cấp các phần mềm tốt hơn nhanh hơn. TDD là dựa trên một ý tưởng đơn giản: các bài kiểm tra Viết cho code của bạn trước khi bạn viết đoạn code riêng của mình. Tuy nhiên, điều này "đơn giản" ý tưởng có kỹ năng và bản án để làm tốt. Bây giờ có một tài liệu hướng dẫn thiết thực để TDD mà sẽ đưa bạn vượt ra ngoài những khái niệm cơ bản. Vẽ trên một...
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Growing Object-Oriented Software, Guided by Tests- P9
- 352 Index events, 78 loops of, 4–5, 8, 40 external, 71, 326–327 on design, 6, 299 listening for, 316–317, 323–325 on failure cases, 41 processed in sequence, 325–326 on implementations, 6 exactly(), jMock, 338 rapid, 317 exceptions, 22 Findbugs, 313 catching, 253–254 fixtures, 23 on hidden threads, 302 functional tests. See acceptance tests runtime, 165 with helpful messages, 330 G Executor interface, 303, 305 garbage collection, 23, 91, 101, 192–194 “Expect Unexpected Changes” principle, 45 getBody(), Smack, 222 Expectation jMock class, 64 getColumnCount(), Swing, 158 ExpectationCounter jMock class, 330 getValueAt(), Swing, 158 expectations, 18, 27, 64–66, 146, 254–255, 277–279, 338 H blocks of, 337, 339 Hamcrest library, 21, 24–25, 95, 268, 274, checking after test’s body, 271 296, 322, 333, 340, 343–345 clear descriptions of, 25 hasColumnTitles(), 169 narrowness of, 255, 277–283 hasEnoughColumns(), 156–157 order of, 128, 282, 341–342 hashCode(), java.lang.Object, 154 quantity of, 242–244, 252 hasProperty(), Hamcrest, 178 specifying actions to perform, 341 hasReceivedBid(), 106–107 Expectations jMock class, 66, 337, 340 hasReceivedJoinRequestFrom(), 109, 176 ExpectationSet jMock class, 330 hasReceivedJoinRequestFromSniper(), ExpectationValue jMock class, 330 106–108 expectFailureWithMessage(), 222 hasShownSniperHasWon(), 323 expectSniperToFailWhenItIs(), 219, 253 hasShownSniperIsBidding(), 106, 110 hasShownSniperIsLosing(), 206–207 F hasShownSniperIsWinning(), 140, 176, 323 failed(), 219 hasTitle(), 169 failure messages, 268–269, 276 helper methods, 7, 51, 66, 162, 166, 210, clearness of, 42 226, 253, 263, 280 self-explanatory, 24–25, 343 naming, 51, 162 failures, 41 Hibernate, 48, 289, 294 detecting, 217–218 HTTP (HyperText Transfer Protocol), 81 diagnostics for, 267–273, 297, 302–307, 332 I displaying, 218–219 IDEs handling, 215–226 filling in missing methods on request, 119 messages about, 255 navigation in, 114 recording, 221–225, 291 IETF (Internet Engineering Task Force), 77 writing down while developing, 41 ignoring(), jMock, 145, 278–279, 339 FakeAuctionServer class, 86, 89, 92–95, ignoringAuction(), 219 107–110, 120, 176, 194, 254, 276 IllegalArgumentException, 22 FeatureMatcher Hamcrest class, 162, 178 implementations feedback, 4, 229, 233 feedback on, 6 from automated deployment, 35–36 independent of context, 244 incremental, 300 null, 130, 136, 180, 218
- Index 353 index cards syntax noise of, 253 for technical tasks to be addressed, 41 using compiler to navigate dependencies, for to-do lists, 80–81, 103, 120–121, 225 130–131, 148, 171, 182, 201, Java EE (Java Platform, Enterprise Edition), 211–212, 225 293–294, 301 information hiding, 49, 55–56 Java Servlet API, 330 initializers, 23 JAXB (Java API for XML Binding), 289 inSequence(), jMock, 338, 341 JButton Swing component, 185 instanses, 237–238 JDBC (Java Database Connectivity), 294 integration tests, 9–10, 186–188 JDO (Java Data Objects), 289 and threads, 71 JFormattedTextField Swing component, 208 difficult to code, 44 JFrame Swing component, 96 for adapters, 70 JFrameDriver WindowLicker class, 91 for persistence implementations, 300 JIDs (Jabber IDs), 77, 197 passing, 40 JLabel Swing component, 150 speed of, 300 jMock library, 24–27, 274, 332 IntelliJ IDEA, 119, 250 allowances in, 146 interface discovery, 19 double braces in, 337 interfaces, 14, 58, 61 expectations in, 25, 64–66, 146 callback, 71 extensions to, 162 implementing, 63–64 generating messages in, 345 mocking, 235 states in, 145 naming, 63–64, 237, 297 using for stress tests, 307 narrowness of, 63 verifying mock objects in, 24 pulling, 61, 63 version 2, 21, 25–27, 333, 335–342 refactoring, 63–64 JMS (Java Messaging Service), 292 relationships with, 63 JMSTransactor class, 292 segregating, 236 joinAuction(), 100, 131–132, 142, invocations 180–182, 187–188, 192, 208 allowed, 27, 146 JPA (Java Persistence API), 279, 289, 294 constrained, 342 persistence identifiers in, 295 counting, 338–339 JTA (Java Transaction API), 292 expected, 27, 146 JTable Swing component, 52, 149–157, 170 number of, 27 JTATransactor class, 292–293 order of, 279–282, 341 JTextField Swing component, 185 invokeAndWait(), Swing, 100, 180 JUnit library, 84, 274, 332–333 invokeLater(), Swing, 100 generating messages in, 345 isForSameItemAs(), 181 new instances for each test in, 22, 117 isSatisfied(), WindowLicker, 320–321 version 4.5, 24 Item class, 209–211, 213 version 4.6, 21, 335 iteration zero, 83, 102 JUnit4Mockery jMock class, 336 J L Jabber. See XMPP Law of Demeter. See “Tell, Don’t Ask” Java programming language, 21 principle arrays in, 177 Lisp programming language, 66 collections in, 179 literals. See values logging framework in, 223 locks, 302, 318 method overloading in, 261 log files, 221–225, 291 package loops in, 191 cleaning up before testing, 221 synchronization errors in, 313 generating, 223
- 354 Index Logger class, 223–224, 237 testing, 43 logging, 233–235 See also helper methods amount of, 235 MissingValueException, 218 diagnostic, 233–235 mock objects, 18–20, 25–27 isolated in a separate class, 226 creating, 336 LoggingXMPPFailureReporter class, 223–224 for third-party code, 69–71, 157, 300 LTSA tool, 302, 313 history of, 329–333 invocation order of, 279–282 M naming, 336 Main class, 91, 101, 108, 117–118, 123, 126, to visualize protocols, 58, 61 132–134, 142, 168, 178–180, 183, 185, mockery, 20, 25 188–203 Mockery jMock class, 26, 64, 66, 307, 336 matchmaker role of, 191 mocking main(), 91, 96 adjustments, 58 MainWindow class, 96, 100, 113, 134, 151, classes, 223–224, 235–237 156, 166–167, 185–187, 199, 208–209 dependencies, 58 MainWindowTest class, 186, 209 interfaces, 235 makeControls(), 184–185 notifications, 58 Mars Climate Orbiter disaster, 59 peers, 58 Matcher interface, 25, 268, 343–345 returned types, 279 matchers, 24–25, 95, 155, 157, 276, 322, third-party code, 237 339–340 values, 237–238 combining, 24 Moon program, 41 custom, 25, 178, 296, 340, 343–345 multithreading. See threads reversing, 24 stateless, 344 N Matchers Hamcrest class, 340 .Net, 22, 232 matches(), Hamcrest, 343 “Never Pass Null between Objects” meetings, 4 principle, 274 MessageHandler class, 217 never(), jMock, 339 MessageListener interface, 93–94, 99, NMock library, 332 112–115, 129, 219 not(), Hamcrest, 24, 340 messages, 13, 17 notifications, 52–53, 126, 192 between objects, 50, 58 capturing, 318–320 creating and checking in the same mocking, 58 construct, 109 order of, 280 parsing, 118–120 recording, 324 See also failure messages notifiesAuctionClosedWhenCloseMessage- methods, 13 Received(), 114 calling, 65 notifiesAuctionFailedWhenBadMessage- order of, 128 Received(), 217 expected, 339–340 notifiesAuctionFailedWhenEventType- factory, 257–258, 260–261 Missing(), 218 getter, 329–330 notifiesBidDetailsWhenCurrentPrice- grouping together, 176 MessageReceivedFromOtherBidder(), ignoring, 279 141 naming, 86, 173, 250 notifiesBidDetailsWhenCurrentPrice- overloading, 261 MessageReceivedFromSniper(), 141 side effects of, 51 notToBeGCd field, 101, 179, 197, 200, 203 “sugar,” 65–66 NullPointerException, 53, 274 NUnit library, 22, 117, 332
- Index 355 O Poller class, 320–321 object mother pattern, 257–258 polling for changes, 317, 320–321, 323–325 object-oriented programming, 13, 329 PortfolioListener interface, 199 objects ports, 48 abstraction level of, 57 “ports and adapters” architecture, 48, 201, bringing out relationships between, 236 284, 297 collaborating, 18–20, 52–53, 58, 60–62, PriceSource enumeration, 141, 148 186 Probe interface, 320–322 communicating, 13–14, 50, 58, 244–245 probing a system, 315, 320–322 composite, 53–54 processMessage(), Smack, 114–115, context-independent, 54–55, 233 135–136, 217, 219 created by builders, 259–260 production environment, 95 difficult to decouple, 273 programming styles, 51 mutable, 14 progress measuring, 4, 40 sharing references to, 50 PropertyMatcher Hamcrest class, 178 naming, 62, 244 null, 22, 115, 130, 242 Q observable invariants with respect to queries, 278 concurrency of, 306 passive, 311–312 R persistent, 298–299 receivesAMessageMatching(), 108 simplifying, 55 redesign, 7 single responsibility of, 51–52 refactoring, 5–7 states of, 13, 59, 145–146, 281–283, 299, code difficult to test, 44–45 306, 342 importance of, during TDD, 225–226 subordinate, 254, 291–292, 311 incremental, 202 tracer, 270–271 writing down while developing, 41 validity of, 53 reference types, 269 vs. values, 13–14, 51, 59 regression suites, 6, 40 web of, 13, 64–65 regression tests, 5 oneOf(), jMock, 278, 337–338 releases, 4, 9 Openfire, 86, 89, 95 planning, 81 ORM (Object/Relational Mapping), 289, to a production system, 35 297, 299 removeMessageListener(), Smack, 220 reportPrice(), 106–107, 176 P reportsInvalidMessage(), 216, 221 packages reportsLostIfAuctionClosesImmediately(), loops of, 191 145 single responsibility of, 52 reportsLostIfAuctionClosesWhenBidding(), pair programming, 4 146 patterns, naming after, 297 repository pattern, 297 peers, 50 resetLogging(), 223 mocking, 58 responsibilities, 16, 171, 220, 222 types of, 52–53 quantity of, 61, 240–241, 332 persistence tests, 289–300 See also “single responsibility” principle and transactions, 292–294 reverting changes, 267 cleaning up at the start, 291 rock climbing, 202 failure diagnostics in, 297 roles, 16 isolating from one another, 290–292 rollback(), 279 round-trip, 297–300 rolling back, 267 slowness of, 300 Ruby programming language, 331
- 356 Index Rule annotation, 24 SnipersTableModel class, 149, 151–152, 156, RuntimeException, 255, 277 166, 168, 170–171, 180–182, 185, runUntilIdle(), 304 197–201, 207 @RunWith annotation, 23, 26, 336 SniperState class, 155, 158–161, 207, 216, 278 S sniperStateChanged(), 156–164, 278 safelyAddItemToModel(), 180, 188 SniperStateDisplayer class, 133, 147, 155, same(), jMock, 340 167–168 sample(), WindowLicker, 320–321 sniperWinning(), 143, 162–163 scheduled activities, 326–327 sniperWinsAnAuctionByBiddingHigher(), Scrum projects, 1 139 SelfDescribing interface, 343 sniperWon(), 147, 164 sendInvalidMessageContaining(), 216 Spring, 294 Sequence jMock class, 341–342 startBiddingFor(), 184 sequences, 279–282, 341–342 startBiddingIn(), 177 servlets, 301, 311 startBiddingWithStopPrice(), 206–207 setImposteriser(), jMock, 223 startSellingItem(), 92, 176 setStatusText(), 166 startSniper(), 183–184 [Setup] methods, 22 startsWith(), Hamcrest, 343–345 showsSniperHasFailed(), 216 state machines, 279–282, 342 showsSniperHasWonAuction(), 140, 176 state transition diagrams, 212 showsSniperStatus(), 91–92 States jMock class, 146, 198, 281–283 “single responsibility” principle, 51–52, 113, static analysis tools, 313 123, 125, 220, 222 stop price, 80, 205–213 SingleMessageListener class, 93–94, stress tests, 306–313 107–108 failing, 308–309, 313 singleton pattern, 50, 230 on event processing order, 326 Smack library, 86 on passive objects, 311–312 exceptions in, 217 running in different environments, 313 threads in, 93, 301 strings Smalltalk programming language checking if starts with a given prefix, cascade, 258, 330, 332 343–345 programming style compared to Java, 330 comparing, 14 Sniper application. See Auction Sniper vs. domain types, 213, 262, 269 Sniper class, 62 StringStartsWithMatcher Hamcrest class, sniperAdded(), 203 345 sniperBidding(), 126–128, 155, 160–162 stubs, 84, 243, 277, 339 SniperCollector class, 62, 198–199, 245 success cases, 41 sniperForItem(), 198 Swing SniperLauncher class, 62, 197–199, 210 manipulating features in, 90 SniperListener interface, 124–126, 133, testing, 86–87 154–155, 163–164, 168 threads in, 123, 133, 180, 301 sniperLost(), 125, 147, 164 SwingThreadSniperListener interface, 168, sniperMakesAHigherBidButLoses(), 139 197, 199 SniperPortfolio class, 199–203 Synchroniser jMock class, 307–308, sniperReportsInvalidAuctionMessageAnd- 312–313 StopsRespondingToEvents(), 216 synchronizations, 301–314 SniperSnapshot class, 159–164, 173, errors in, 302 180–181, 198–199, 211, 219, 278 testing, 302, 306–310, 313 vs. assertions, 326
- Index 357 system failing, 267–273 application model of, 48 flexibility of, 273–285 changing behavior of, 48, 55 flickering, 317 concurrency architecture of, 301–302 focused, 273, 277, 279, 279 maintainability of, 47 for late integration, 36 public drawings of, during development, hierarchy of, 9–10 34 maintaining, 247, 273–274 returning to initial state after a test, 323 naming, 44, 248–250, 252, 264, 268, 326 simplifying, 112 readability of, 247–257, 273, 280 system tests. See acceptance tests repeatability of, 23 runaway, 322–323 T running, 6 tableChanged(), Swing, 157, 181 sampling, 316–317, 320–325 TableModel class, 149, 168–171 self-explanatory, 274–275 TableModelEvent class, 157, 180–181 separate packages for, 114 TableModelListener class, 156–157 size of, 45, 268 task runners, 303 states of, 283 TDD (Test-Driven Development), 1, 5, 229 synchronizing, 301–314, 317 cycle of, 6, 39–45, 271–272 with background threads, 312–313 for existing systems, 37 tightly coupled, 273 golden rule of, 6 triggering detectable behavior, 325 kick-starting, 31–37 writing, 6 sustainable, 227–285 backwards, 252 [TearDown] methods, 22 in a standard form, 251–252 “Tell, Don’t Ask” principle, 17, 54, 245 See also acceptance tests, end-to-end tests, template methods, 344 integration tests, persistence tests, test data builders, 238, 258–259 unit tests calling within transactions, 300 textFor(), 166 combining, 261, 300 “the simplest thing that could possibly creating similar objects with, 259–260 work,” 41 lists of, 298–299 then(), jMock, 281–282, 338, 342 removing duplication with, 262–264 third-party code, 69–72 wrapping up in factory methods, 261 abstractions over, 10 test runner, 23–24 mocking, 69–71, 157, 237, 300 JMock, 26 patching, 69 Parameterized, 24 testing integration with, 186–188, 289 “test smells,” 229, 235, 248 value types from, 71 benefits of listening to, 244–246 Thor Automagic, 12 @Test annotation, 22 threads, 71, 301–315 TestDox convention, 249–250 scheduling, 313 Test-Driven Development. See TDD three-point contact, 202 tests time boxes, 4 against fake services, 84, 88, 93 Timeout class, 318, 322 against real services, 32, 88, 93 timeouts, 230, 312–313, 316–318 asynchronous, 315–327 timestamps, 276 at the beginning of a project, 36, 41 toString(), java.lang.Object, 154 brittleness of, 229, 255, 257, 273 tracer object, 270–271 cleaning up, 245, 248, 273 “train wreck” code, 17, 50–51, 65 decoupling from tested objects, 278 transaction management, 294 dependencies in, 275 transactors, 292–293 explicit constraints in, 280 translate(), 217
- 358 Index translatorFor(), 220, 226, 253 values, 255–256 TypeSafeMatcher Hamcrest class, comparing, 22 344 expected, 127 immutable, 50, 59 U mocking, 237–238 unit tests, 4, 9 mutable, 50 against static global objects, 234 obviously canned, 270 and threads, 301–314 self-describing, 269, 285 at the beginning of a project, 43 side effects of, 51 breaking dependencies in, 233 vs. objects, 13–14, 51, 59 brittleness of, 245 variables, 255–256 difficult to code, 44 global, 50 failing, 8 naming, 209, 330 isolating from each other, 22, 117 length of, 245–246 W limiting scope of, 57 waitForAnotherAuctionEvent(), 216 naming, 114, 141 waitUntil(), 326 on behavior, not methods, 43 walking skeleton, 32–37 on collaborating objects, 18–20 for Auction Sniper, 79, 83–88 on synchronization, 302, 306–310, 313 when(), jMock, 281–282, 338, 342 passing, 40 whenAuctionClosed(), 164–165 readability of, 245–246 will(), jMock, 338, 341 simplifying, 62 WindowAdapter class, 134 speed of, 300, 312 WindowLicker library, 24, 86–87, 186–187, structure of, 335–342 254, 316 writing, 11 controlling Swing components in, 90–91 Unix, 66 error messages in, 96 User Experience community, 81, 212 with(), jMock, 339–340 user interface overloaded, 261 configuring through, 242 dependencies on, 113 X handling user requests, 186 XmlMarshaller class, 284–285 support logging in, 233 XmlMarshallerTest class, 284 working on parallel to development, 183, XMPP (eXtensible Messaging and Presence 212 Protocol), 76–77, 105, 203 UserRequestListener interface, 186–188, messages in, 301 208–209, 213 reliability of, 81 security of, 81 V XMPP message brokers, 84, 86, 95 value types, 59–60, 141 XMPPAuction class, 62, 131–132, 192–197, from third-party code, 71 203, 224 helper, 59 XMPPAuctionException, 224 naming, 173 XMPPAuctionHouse class, 62, 196–197, 203, placeholder, 59, 209 224 public final fields in, 154 XMPPConnection class, 195–197 vs. values, 59 XMPPException, 130 with generics, 136 XMPPFailureReporter class, 222–223, 226 valueIn(), 166–167 XP (Extreme Programming), 1, 41, 331 ValueMatcherProbe WindowLicker class, 187 XStream, 289 XTC (London Extreme Tuesday Club), 331
- informIT.com THE TRUSTED TECHNOLOGY LEARNING SOURCE InformIT is a brand of Pearson and the online presence for the world’s leading technology publishers. It’s your source for reliable and qualified content and knowledge, providing access to the top brands, authors, and contributors from the tech community. LearnIT at InformIT Looking for a book, eBook, or training video on a new technology? Seeking timely and relevant information and tutorials? Looking for expert opinions, advice, and tips? InformIT has the solution. • Learn about new releases and special promotions by subscribing to a wide variety of newsletters. Visit informit.com /newsletters. • Access FREE podcasts from experts at informit.com /podcasts. • Read the latest author articles and sample chapters at informit.com /articles. • Access thousands of books and videos in the Safari Books Online digital library at safari.informit.com. • Get tips from expert blogs at informit.com /blogs. Visit informit.com /learn to discover all the ways you can access the hottest technology content. Are You Part of the IT Crowd? Connect with Pearson authors and editors via RSS feeds, Facebook, Twitter, YouTube, and more! Visit informit.com /socialconnect. informIT.com THE TRUSTED TECHNOLOGY LEARNING SOURCE
- FREE Online Edition Your purchase of Growing Object-Oriented Software, Guided by Tests includes access to a free online edition for 45 days through the Safari Books Online subscription service. Nearly every Addison-Wesley Professional book is available online through Safari Books Online, along with more than 5,000 other technical books and videos from publishers such as Cisco Press, Exam Cram, IBM Press, O’Reilly, Prentice Hall, Que, and Sams. SAFARI BOOKS ONLINE allows you to search for a specific answer, cut and paste code, download chapters, and stay current with emerging technologies. Activate your FREE Online Edition at www.informit.com/safarifree STEP 1: Enter the coupon code: CYMIQVH. STEP 2: New Safari users, complete the brief registration form. Safari subscribers, just log in. If you have difficulty registering on Safari or accessing the online edition, please e-mail customer-service@safaribooksonline.com
CÓ THỂ BẠN MUỐN DOWNLOAD
Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline: 093 303 0098
Email: support@tailieu.vn