Enterprise Java developers must achieve broader, deeper test coverage, going beyond unit testing to implement functional and integration testing with systematic acceptance. Next Generation Java(TM) Testing introduces breakthrough Java testing techniques and TestNG, a powerful open source Java testing platform. Cedric Beust, TestNG's creator, and leading Java developer Hani Suleiman, present powerful, flexible testing patterns that will work with virtually any testing tool, framework, or language. They show how to leverage key Java platform improvements designed to facilitate effective testing, such as dependency injection and mock objects. They also thoroughly introduce TestNG, demonstrating how it overcomes the limitations of older frameworks and enables new techniques, making it far easier to test today's complex software systems. Pragmatic and results-focused, Next Generation Java(TM) Testing will help Java developers build more robust code for today's mission-critical environments. This book Illuminates the tradeoffs associated with testing, so you can make better decisions about what and how to test Introduces TestNG, explains its goals and features, and shows how to apply them in real-world environments Shows how to integrate TestNG with your existing code, development frameworks, and software libraries Demonstrates how to test crucial code features, such as encapsulation, state sharing, scopes, and thread safety Shows how to test application elements, including JavaEE APIs, databases, Web pages, and XML files Presents advanced techniques: testing partial failures, factories, dependent testing, remote invocation, cluster-based test farms, and more Walks through installing and using TestNG plug-ins for Eclipse, and IDEA Contains extensive code examples Whether you use TestNG, JUnit, or another testing framework, the testing design patterns presented in this book will show you how to improve your tests by giving you concrete advice on how to make your code and your design more testable. Cover......Page 1 Contents......Page 6 Foreword......Page 14 Preface......Page 16 Acknowledgments......Page 22 About the Authors......Page 24 Chapter 1 Getting Started......Page 26 Stateful Classes......Page 28 Exceptions Are Not That Exceptional......Page 29 Running Tests......Page 30 Dependencies......Page 31 JUnit 4......Page 32 Object-Oriented Programming and Encapsulation......Page 33 The Design Patterns Revolution......Page 34 Identifying the Enemy......Page 35 Recommendations......Page 41 Annotations......Page 42 Tests, Suites, and Configuration Annotations......Page 43 Groups......Page 45 Conclusion......Page 46 Testing for Failures......Page 48 Reporting Errors......Page 49 Runtime and Checked Exceptions......Page 50 Testing Whether Your Code Handles Failures Gracefully......Page 52 When Not to Use expectedExceptions......Page 56 testng-failed.xml......Page 57 Factories......Page 59 @Factory......Page 60 org.testng.ITest......Page 63 Data-Driven Testing......Page 64 Parameters and Test Methods......Page 67 Passing Parameters with testng.xml......Page 69 Passing Parameters with @DataProvider......Page 72 The Method Parameter......Page 75 The ITestContext Parameter......Page 77 Lazy Data Providers......Page 79 Pros and Cons of Both Approaches......Page 84 Supplying the Data......Page 85 Data Provider or Factory?......Page 87 Tying It All Together......Page 88 Asynchronous Testing......Page 92 Testing Multithreaded Code......Page 96 Concurrent Testing......Page 97 threadPoolSize, invocationCount, and timeOut......Page 100 Concurrent Running......Page 104 Turning on the Parallel Bit......Page 107 Performance Testing......Page 108 Algorithm Complexity......Page 109 Testing Complexity......Page 112 Mocks versus Stubs......Page 115 Designing for Mockability......Page 120 Mock Libraries......Page 121 Selecting the Right Strategy......Page 124 Mock Pitfalls......Page 125 Dependent Testing......Page 128 Dependent Code......Page 129 Dependent Testing with TestNG......Page 130 Deciding Whether to Depend on Groups or on Methods......Page 131 Failures of Configuration Methods......Page 135 The Problem......Page 138 Pitfalls of Inheritance......Page 141 Test Groups......Page 144 Syntax......Page 145 Groups and Runtime......Page 147 Running Groups......Page 150 Using Groups Effectively......Page 152 Code Coverage......Page 157 A Coverage Example......Page 158 Coverage Metrics......Page 159 Coverage Tools......Page 161 Implementation......Page 171 A Guide to Successful Coverage......Page 172 Conclusion......Page 175 Chapter 3 Enterprise Testing......Page 178 A Typical Enterprise Scenario......Page 179 Testing Methodology......Page 180 Issues with the Current Approach......Page 181 A Concrete Example......Page 182 Goals......Page 184 Test Implementation......Page 185 Testing for Success......Page 186 Building Test Data......Page 188 Test Setup Issues......Page 191 Error Handling......Page 197 Emerging Unit Tests......Page 200 Coping with In-Container Components......Page 202 Putting It All Together......Page 203 The Pattern......Page 207 The Test......Page 209 The Role of Refactoring......Page 211 A Concrete Example......Page 212 An In-Container Approach......Page 218 Conclusion......Page 219 Chapter 4 Java EE Testing......Page 222 In-Container versus Out-of-Container Testing......Page 223 Creating a Test Environment......Page 225 Identifying Tests......Page 226 Registering Tests......Page 228 Registering a Results Listener......Page 229 Understanding JNDI's Bootstrapping......Page 232 Spring's SimpleNamingContextBuilder......Page 234 Java Database Connectivity (JDBC)......Page 235 c3p0......Page 237 Spring......Page 238 Java Transaction API (JTA)......Page 240 Java Open Transaction Manager (JOTM)......Page 242 Atomikos TransactionEssentials......Page 243 Creating a Sender/Receiver Test......Page 244 Using ActiveMQ for Tests......Page 246 Java Persistence API (JPA)......Page 250 Configuring the JPA Provider......Page 252 Writing the Test......Page 254 Simulating a Container......Page 255 Using Spring as the Container......Page 256 Enterprise Java Beans 3.0 (EJB3)......Page 261 Message-Driven Beans......Page 262 Session Beans......Page 265 Another Spring Container......Page 268 Disadvantages of a Full Container......Page 269 Java API for XML Web Services (JAX-WS)......Page 271 Setting Up the Test Environment......Page 273 Creating the Service Test......Page 276 XPath Testing......Page 278 Testing Remote Services......Page 279 Mock/Stub Objects......Page 280 Embedded Container......Page 282 In-Memory Invocation......Page 285 XML......Page 287 Using dom4j......Page 288 Using XMLUnit......Page 289 Conclusion......Page 291 Chapter 5 Integration......Page 294 Spring......Page 295 Spring's Test Package Features......Page 296 Test Class Hierarchy......Page 297 The Issue with Spring......Page 305 Enter Guice......Page 306 A Typical Dependency Scenario......Page 307 The Object Factory......Page 309 Guice Configuration......Page 311 Guice-Based Test......Page 315 Grouping Test Dependencies......Page 316 Injecting Configuration......Page 318 Configuration......Page 320 Usage......Page 322 Verifying Results......Page 324 HtmlUnit......Page 328 Configuration......Page 329 Usage......Page 330 Selenium......Page 335 Testing Approach......Page 337 Configuration......Page 338 Usage......Page 339 Tests for Painting Code......Page 341 CI Server Features......Page 345 TestNG Integration......Page 346 Conclusion......Page 347 org.testng.TestNG, ITestResult, ITestListener, ITestNGMethod......Page 350 A Concrete Example......Page 353 The XML API......Page 356 Synthetic XML Files......Page 358 BeanShell Overview......Page 360 TestNG and BeanShell......Page 362 Interactive Execution......Page 364 Method Selectors......Page 366 Annotation History......Page 371 Using TestNG Annotation Transformers......Page 373 Possible Uses of Annotation Transformers......Page 378 Default Reports......Page 380 The Report Plug-in API......Page 385 Writing Custom Annotations......Page 391 Implementation......Page 392 Testing......Page 396 Conclusion......Page 400 Motivation......Page 402 The Care and Feeding of Exceptions......Page 403 Immutable State......Page 407 Mutable State......Page 408 TDD Promotes Microdesign over Macrodesign......Page 410 TDD Is Hard to Apply......Page 411 Testing Private Methods......Page 413 Testing versus Encapsulation......Page 416 The Power of Debuggers......Page 417 Logging Best Practices......Page 419 The Value of Time......Page 422 Conclusion......Page 424 Installing the Plug-in......Page 426 Creating a Launch Configuration......Page 429 Converting JUnit Tests......Page 435 Installing the Plug-in......Page 436 Running Tests......Page 437 Running Shortcuts......Page 442 Viewing Test Results......Page 443 Running Plug-in Refactorings......Page 444 JDK 1.4 and JDK 5......Page 446 Annotation Javadocs......Page 448 @DataProvider/@testng.data-provider......Page 450 @Parameters/@testng.parameters......Page 451 @Test/@testng.test......Page 452 The org.testng.TestNG Class......Page 453 The XML API......Page 457 Appendix C: testng.xml......Page 460 Overview......Page 461 ......Page 462 and ......Page 465 ......Page 466 and ......Page 467 , , , and ......Page 468 ......Page 469 and ......Page 471 ......Page 472 From the Command Line......Page 474 From ant......Page 477 Eclipse......Page 478 IDEA......Page 479 Incremental Migration and JUnit Mode......Page 480 Converting JUnit Code......Page 481 Assertions......Page 482 Running a Single Test......Page 483 Maintaining State between Invocations......Page 486 The AllTests Pattern......Page 488 Testing Exceptions......Page 492 The Parameterized Test Case Pattern......Page 494 B......Page 496 C......Page 497 D......Page 498 E......Page 499 I......Page 500 J......Page 501 N......Page 503 R......Page 504 S......Page 505 T......Page 506 X......Page 507 This is the eBook version of the printed book. If the print book includes a CD-ROM, this content is not included within the eBook version. Enterprise Java developers must achieve broader, deeper test coverage, going beyond unit testing to implement functional and integration testing with systematic acceptance. Next Generation JavaTM Testing introduces breakthrough Java testing techniques and TestNG, a powerful open source Java testing platform. Cédric Beust, TestNG's creator, and leading Java developer Hani Suleiman, present powerful, flexible testing patterns that will work with virtually any testing tool, framework, or language. They show how to leverage key Java platform improvements designed to facilitate effective testing, such as dependency injection and mock objects. They also thoroughly introduce TestNG, demonstrating how it overcomes the limitations of older frameworks and enables new techniques, making it far easier to test today's complex software systems. Pragmatic and results-focused, Next Generation JavaTM Testing will help Java developers build more robust code for today's mission-critical environments. This book Illuminates the tradeoffs associated with testing, so you can make better decisions about what and how to test Introduces TestNG, explains its goals and features, and shows how to apply them in real-world environments Shows how to integrate TestNG with your existing code, development frameworks, and software libraries Demonstrates how to test crucial code features, such as encapsulation, state sharing, scopes, and thread safety Shows how to test application elements, including JavaEE APIs, databases, Web pages, and XML files Presents advanced techniques: testing partial failures, factories, dependent testing, remote invocation, cluster-based test farms, and more Walks through installing and using TestNG plug-ins for Eclipse, and IDEA Contains extensive code examples Whether you use TestNG, JUnit, or another testing framework, the testing design patterns presented in this book will show you how to improve your tests by giving you concrete advice on how to make your code and your design more testable.
Enterprise Java developers must achieve broader, deeper test coverage, going beyond unit testing to implement functional and integration testing with systematic acceptance. Next Generation Java™ Testing introduces breakthrough Java testing techniques and TestNG, a powerful open source Java testing platform.
Cédric Beust, TestNG's creator, and leading Java developer Hani Suleiman, present powerful, flexible testing patterns that will work with virtually any testing tool, framework, or language. They show how to leverage key Java platform improvements designed to facilitate effective testing, such as dependency injection and mock objects. They also thoroughly introduce TestNG, demonstrating how it overcomes the limitations of older frameworks and enables new techniques, making it far easier to test today's complex software systems.
Pragmatic and results-focused, Next Generation Java™ Testing will help Java developers build more robust code for today's mission-critical environments.
This book
- Illuminates the tradeoffs associated with testing, so you can make better decisions about what and how to test
- Introduces TestNG, explains its goals and features, and shows how to apply them in real-world environments
- Shows how to integrate TestNG with your existing code, development frameworks, and software libraries
- Demonstrates how to test crucial code features, such as encapsulation, state sharing, scopes, and thread safety
- Shows how to test application elements, including JavaEE APIs, databases, Web pages, and
- Presents advanced techniques: testing partial failures, factories, dependent testing, remoteinvocation, cluster-based test farms, and more
- Walks through installing and using TestNG plug-ins for Eclipse, and IDEA
- Contains extensive code examples
Whether you use TestNG, JUnit, or another testing framework, the testing design patterns presented in this book will show you how to improve your tests by giving you concrete advice on how to make your code and your design more testable.
Enterprise Java developers must achieve broader, deeper test coverage, going beyond unit testing to implement functional and integration testing with systematic acceptance. Next Generation Java TM Testing introduces breakthrough Java testing techniques and TestNG, a powerful open source Java testing platform. Cédric Beust, TestNG's creator, and leading Java developer Hani Suleiman, present powerful, flexible testing patterns that will work with virtually any testing tool, framework, or language. They show how to leverage key Java platform improvements designed to facilitate effective testing, such as dependency injection and mock objects. They also thoroughly introduce TestNG, demonstrating how it overcomes the limitations of older frameworks and enables new techniques, making it far easier to test today's complex software systems. Pragmatic and results-focused, Next Generation Java TM Testing will help Java developers build more robust code for today's mission-critical environments. This book Illuminates the tradeoffs associated with testing, so you can make better decisions about what and how to test Introduces TestNG, explains its goals and features, and shows how to apply them in real-world environments Shows how to integrate TestNG with your existing code, development frameworks, and software libraries Demonstrates how to test crucial code features, such as encapsulation, state sharing, scopes, and thread safety Shows how to test application elements, including JavaEE APIs, databases, Web pages, and XML files Presents advanced techniques: testing partial failures, factories, dependent testing, remote invocation, cluster-based test farms, and more Walks through installing and using TestNG plug-ins for Eclipse, and IDEA Contains extensive code examples Whether you use TestNG, JUnit, or another testing framework, the testing design patterns presented in this book will show you how to improve your tests by giving you concrete advice on how to make your code and your design more testable "Whether you use TestNG, JUnit, or another testing framework, the testing design patterns presented in this book will show you how to improve your tests by giving you concrete advice on how to make your code and your design more testable."--BOOK JACKET