Test Driven: Tdd and Acceptance Tdd for Java Developers

Free Download

Authors:

ISBN: 9781932394856, 1-932394-85-0

Size: 8 MB (8784587 bytes)

Pages: 585/585

File format:

Language:

Publishing Year:

Category:

Lasse Koskela9781932394856, 1-932394-85-0

Learning a new development technique such as test-driven development (TDD) is a bit like learning to program.You study fundamental theories to understand what is going on, apply those theories to verify your understanding, and sometimes return to your textbooks to confirm how to handle some less common situations. This book was written to help you do all this and more in your journey to becoming a craftsman practicing TDD. “Test Driven” is the first one-stop shop for Java developers wanting a comprehensive understanding of how to apply TDD methodology. This book will enable Java developers new to TDD to become seasoned test-driven developers ready to tackle any Java or J2EE project. It takes you from basic theory to practical techniques to tool support and provides how-to’s for applying TDD. It is also covers acceptance test-driven development (ATDD), which applies the ideas of TDD to the overall software development process, implementing features test-first. Java developers can use the book for getting started with TDD and ATDD, and afterwards for help in solving technical problems such as, “how can I test-drive code that uses technology X?” “Test Driven” teaches you the simple rules and underlying dynamics that make TDD such a powerful technique. Along the way the author expertly breaks down those myths that say some J2EE technologies cannot be done test-first, showing how to test-drive J2EE components just like regular Java code. He also gives comprehensive coverage to the latest EJB specification, web components from plain Java Servlets to the coolest open source web framework on the block, and data access solutions from raw JDBC to fancy object-relationalmappingframeworks. In addition to the usual J2EE technologies, you also learn to apply TDD to multithreaded programs and Java Swing too.

Table of contents :
Test Driven……Page 1
contents……Page 10
preface……Page 18
about this book……Page 22
What’s next?……Page 26
Author Online……Page 27
A TDD primer……Page 30
The big picture……Page 32
1.1.1 Creating poorly written code……Page 34
1.1.2 Failing to meet actual needs……Page 35
1.2 Solution: being test-driven……Page 36
1.2.1 High quality with TDD……Page 37
1.2.2 Meeting needs with acceptance TDD……Page 39
1.2.3 What’s in it for me?……Page 40
1.3 Build it right: TDD……Page 43
1.3.1 Test-code-refactor: the heartbeat……Page 44
1.3.2 Developing in small increments……Page 48
1.3.3 Keeping code healthy with refactoring……Page 53
1.3.4 Making sure the software still works……Page 57
1.4.1 What’s in a name?……Page 60
1.4.2 Close collaboration……Page 61
1.4.3 Tests as a shared language……Page 62
1.5.1 Unit-testing with xUnit……Page 65
1.5.3 Continuous integration and builds……Page 66
1.5.4 Code coverage……Page 68
1.6 Summary……Page 70
Beginning TDD……Page 72
2.1.1 Decomposing requirements……Page 74
2.1.3 Working from a test list……Page 76
2.2 Choosing the first test……Page 77
2.2.1 Creating a list of tests……Page 78
2.2.2 Writing the first failing test……Page 79
2.2.3 Making the first test pass……Page 83
2.2.4 Writing another test……Page 85
2.3 Breadth-first, depth-first……Page 87
2.3.1 Faking details a little longer……Page 88
2.3.2 Squeezing out the fake stuff……Page 89
2.4 Let’s not forget to refactor……Page 92
2.4.1 Potential refactorings in test code……Page 93
2.4.2 Removing a redundant test……Page 94
2.5.1 Expecting an exception……Page 95
2.5.2 Refactoring toward smaller methods……Page 97
2.5.3 Keeping methods in balance……Page 98
2.5.4 Expecting details from an exception……Page 99
2.6 Loose ends on the test list……Page 100
2.6.1 Testing for performance……Page 101
2.7 Summary……Page 102
Refactoring in small steps……Page 104
3.1 Exploring a potential solution……Page 105
3.1.2 Learning by writing tests……Page 106
3.1.3 Example spike for learning an API……Page 107
3.2 Changing design in a controlled manner……Page 109
3.2.1 Creating an alternative implementation……Page 110
3.2.2 Switching over safely……Page 115
3.3.1 Keeping things compatible……Page 119
3.3.2 Making the switchover……Page 124
3.4 Summary……Page 127
Concepts and patterns for TDD……Page 128
4.1 How to write tests and make them pass……Page 129
4.1.1 Test-selection strategies……Page 130
4.1.2 Implementation strategies……Page 133
4.1.3 Prime guidelines for test-driving……Page 135
4.2.1 Fixtures are the context for tests……Page 137
4.2.3 State and interaction-based testing……Page 139
4.3.1 Example of a test double……Page 142
4.3.2 Stubs, fakes, and mocks……Page 144
4.3.3 Mock objects in action……Page 145
4.4 Guidelines for testable designs……Page 147
4.4.1 Choose composition over inheritance……Page 148
4.4.2 Avoid static and the Singleton……Page 149
4.4.3 Isolate dependencies……Page 151
4.4.4 Inject dependencies……Page 153
4.5 Unit-testing patterns……Page 156
4.5.1 Assertion patterns……Page 157
4.5.2 Fixture patterns……Page 161
4.5.3 Test patterns……Page 166
4.6 Working with legacy code……Page 173
4.6.1 Test-driven legacy development……Page 174
4.6.2 Analyzing the change……Page 175
4.6.3 Preparing for the change……Page 176
4.7 Summary……Page 177
Applying TDD to specific technologies……Page 180
Test-driving web components……Page 182
5.1 MVC in web applications in 60 seconds……Page 183
5.2.1 Test-driving Java Servlets……Page 185
5.2.2 Test-driving Spring controllers……Page 197
5.3 Creating the view test-first……Page 202
5.3.1 Test-driving JSPs with JspTest……Page 203
5.3.2 Test-driving Velocity templates……Page 208
5.4 TDD with component-based web frameworks……Page 213
5.4.1 Anatomy of a typical framework……Page 214
5.4.2 Fleshing out Wicket pages test-first……Page 215
5.5 Summary……Page 222
Test-driving data access……Page 224
6.1 Exploring the problem domain……Page 225
6.1.1 Data access crosses boundaries……Page 226
6.1.2 Separating layers with the DAO pattern……Page 227
6.2 Driving data access with unit tests……Page 228
6.2.1 Witnessing the tyranny of the JDBC API……Page 229
6.2.2 Reducing pain with Spring’s JdbcTemplate……Page 234
6.2.3 Closer to test-driven nirvana with Hibernate……Page 240
6.3 Writing integration tests before the code……Page 248
6.3.1 What is an integration test?……Page 249
6.3.2 Selecting the database……Page 251
6.4 Integration tests in action……Page 254
6.4.1 Writing our first Hibernate integration test……Page 255
6.4.2 Creating the database schema……Page 259
6.4.3 Implementing the production code……Page 262
6.4.4 Staying clean with transactional fixtures……Page 263
6.5 Populating data for integration tests……Page 264
6.5.1 Populating objects with Hibernate……Page 265
6.5.2 Populating data with DbUnit……Page 266
6.6.1 TDD cycle with integration tests……Page 272
6.6.2 Best of both worlds……Page 273
6.7.1 A tale from the trenches……Page 274
6.7.2 Practices for testable file access……Page 275
6.8 Summary……Page 276
Test-driving the unpredictable……Page 278
7.1.1 Example: logs and timestamps……Page 279
7.1.2 Abstracting system time……Page 281
7.1.3 Testing log output with faked system time……Page 285
7.2 Test-driving multithreaded code……Page 288
7.2.1 What are we testing for?……Page 289
7.2.2 Thread-safety……Page 290
7.2.3 Blocking operations……Page 295
7.2.4 Starting and stopping threads……Page 297
7.2.5 Asynchronous execution……Page 300
7.2.6 Synchronization between threads……Page 303
7.3.1 Semaphores……Page 304
7.3.3 Barriers……Page 305
7.4 Summary……Page 306
Test-driving Swing……Page 308
8.1 What to test in a Swing UI……Page 309
8.1.2 Rendering and layout……Page 310
8.1.3 Interaction……Page 311
8.2 Patterns for testable UI code……Page 312
8.2.2 Supervising Controller……Page 313
8.2.3 Passive View……Page 316
8.3.1 Why do we need tools?……Page 319
8.3.2 TDD-friendly tools……Page 321
8.4 Test-driving a view component……Page 326
8.4.1 Laying out the design……Page 327
8.4.2 Adding and operating standard widgets……Page 329
8.4.3 Drawing custom graphics……Page 333
8.4.4 Associating gestures with coordinates……Page 343
8.5 Summary……Page 348
Building products with Acceptance TDD……Page 350
Acceptance TDD explained……Page 352
9.1.2 Power of storytelling……Page 354
9.1.3 Examples of user stories……Page 355
9.2.1 Example tests for a story……Page 356
9.2.2 Properties of acceptance tests……Page 357
9.2.3 Implementing acceptance tests……Page 362
9.3.1 The acceptance TDD cycle……Page 363
9.3.2 Acceptance TDD inside an iteration……Page 372
9.4.1 Defining the customer role……Page 377
9.4.3 How many testers do we need?……Page 379
9.5.1 Definition of “done”……Page 380
9.5.2 Cooperative work……Page 382
9.5.5 Filling the gap……Page 383
9.6.1 Should we test against the UI?……Page 384
9.6.2 Should we stub parts of our system?……Page 386
9.6.3 Should we test business logic directly?……Page 387
9.7.1 Table-based frameworks……Page 388
9.7.3 Scripting language-based frameworks……Page 390
9.8 Summary……Page 391
Creating acceptance tests with Fit……Page 393
10.1 What’s Fit?……Page 394
10.1.1 Fit for acceptance TDD……Page 395
10.1.2 Test documents contain fixture tables……Page 398
10.1.3 Fixtures: combinations of tables and classes……Page 400
10.2 Three built-in fixtures……Page 401
10.2.1 ColumnFixture……Page 402
10.2.2 RowFixture……Page 405
10.2.3 ActionFixture……Page 408
10.2.4 Extending the built-in fixtures……Page 411
10.3.1 DoFixture……Page 413
10.3.2 SetUpFixture……Page 417
10.4 Executing Fit tests……Page 419
10.4.2 Placing all tests in a folder structure……Page 420
10.4.3 Testing as part of an automated build……Page 421
10.5 Summary……Page 423
Strategies for implementing acceptance tests……Page 425
11.1 What should acceptance tests test?……Page 426
11.1.1 Focus on what’s essential……Page 427
11.1.2 Avoid turbulent interfaces……Page 428
11.1.3 Cross the fence where it is lowest……Page 429
11.2.1 Going end-to-end……Page 430
11.2.2 Crawling under the skin……Page 433
11.2.3 Exercising the internals……Page 436
11.2.4 Stubbing out the irrelevant……Page 438
11.3 Technology-specific considerations……Page 440
11.3.1 Programming libraries……Page 441
11.3.2 Faceless, distributed systems……Page 442
11.3.3 Console applications……Page 444
11.3.4 GUI applications……Page 446
11.3.5 Web applications……Page 450
11.4 Tips for common problems……Page 454
11.4.1 Accelerating test execution……Page 455
11.4.2 Reducing complexity of test cases……Page 460
11.4.3 Managing test data……Page 461
11.5 Summary……Page 463
Adopting TDD……Page 464
12.1.1 Getting it……Page 465
12.1.2 Sense of urgency……Page 466
12.1.4 Exhibiting integrity……Page 467
12.1.5 Time for change……Page 468
12.2 Getting others aboard……Page 469
12.2.1 Roles and ability to lead change……Page 470
12.2.2 Change takes time……Page 472
12.3.1 Recognizing resistance……Page 473
12.3.2 Three standard responses to resistance……Page 477
12.3.3 Techniques for overcoming resistance……Page 478
12.3.4 Picking our battles……Page 482
12.4.1 Evangelize……Page 483
12.4.2 Lower the bar……Page 486
12.4.3 Train and educate……Page 487
12.4.4 Share and infect……Page 488
12.4.5 Coach and facilitate……Page 490
12.4.6 Involve others by giving them roles……Page 492
12.4.7 Destabilize……Page 493
12.5 Summary……Page 494
apendix A: Brief JUnit 4 tutorial……Page 496
apendix B: Brief JUnit 3.8 tutorial……Page 499
apendix C: Brief EasyMock tutorial……Page 502
apendix D: Running tests with Ant……Page 504
D.1 Project directory structure……Page 505
D.2 The basics: compiling all source code……Page 506
D.3 Adding a target for running tests……Page 507
D.4 Generating a human-readable report……Page 508
resources……Page 510
index……Page 516
Bonus Chapter: Test-driving EJB components……Page 543

Reviews

There are no reviews yet.

Be the first to review “Test Driven: Tdd and Acceptance Tdd for Java Developers”
Shopping Cart
Scroll to Top