Paperback(3rd ed.)
-
PICK UP IN STORECheck Availability at Nearby Stores
Available within 2 business hours
Related collections and offers
Overview
Summary
JUnit is the gold standard for unit testing Java applications. Filled with powerful new features designed to automate software testing, JUnit 5 boosts your productivity and helps avoid debugging nightmares. Whether you're just starting with JUnit or you want to ramp up on the new features, JUnit in Action, Third Edition has you covered. Extensively revised with new code and new chapters, JUnit in Action, Third Edition is an up-to-date guide to smooth software testing. Dozens of hands-on examples illustrate JUnit 5's innovations for dependency injection, nested testing, parameterized tests, and more. Throughout, you’ll learn how to use JUnit 5 to automate your testing, for a process that consumes less resources, and gives you more time for developing.
Purchase of the print book includes a free eBook in PDF, Kindle, and ePub formats from Manning Publications.
About the technology
The JUnit framework is the gold standard for unit testing Java applications—and knowing it is an essential skill for Java developers. The latest version, JUnit 5, is a total overhaul, now supporting modern Java features like Lambdas and Streams.
About the book
JUnit in Action, Third Edition has been completely rewritten for this release. The book is full of examples that demonstrate JUnit's modern features, including its new architecture; nested, tagged, and dynamic tests; and dependency injection. You'll benefit from author Catalin Tudose's unique "pyramid" testing strategy, which breaks the testing process into layers and sets you on the path to bug-free code creation.
What's inside
Migrating from JUnit 4 to 5
Effective test automation
Test-driven development and behavior-driven development
Using mocks for test isolation
Connecting JUnit 5 with Maven or Gradle
About the reader
For intermediate Java developers.
About the author
Catalin Tudose has a Ph.D. in Computer Science, and over 15 years of experience as a Senior Java Developer and Technical Team Lead. Previous editions were authored by Petar Tahchiev, Felipe Leme, Gary Gregory, and Vincent Massol.
Table of Contents
PART 1 - JUNIT
1 JUnit jump-start
2 Exploring core JUnit
3 JUnit architecture
4 Migrating from JUnit 4 to JUnit 5
5 Software testing principles
PART 2 - DIFFERENT TESTING STRATEGIES
6 Test quality
7 Coarse-grained testing with stubs
8 Testing with mock objects
9 In-container testing
PART 3 - WORKING WITH JUNIT 5 AND OTHER TOOLS
10 Runing JUnit tests from Maven 3
11 Running JUnit tests from Gradle 6
12 JUnit 5 IDE support
13 Coninuous integration with JUnit 5
PART 4 - WORKING WITH MODERN FRAMEWORKS AND JUNIT 5
14 JUnit 5 extension model
15 Presentation-layer testing
16 Testing Spring applications
17 Testing Spring Boot applications
18 Testing a REST API
19 Testing database applications
PART 5 - DEVELOPING APPLICATIONS WITH JUNIT 5
20 Test-driven development with JUnit 5
21 Behavior-driven development in JUnit 5
22 Implementing a test pyramid strategy with JUnit 5
Product Details
ISBN-13: | 9781617297045 |
---|---|
Publisher: | Manning |
Publication date: | 12/08/2020 |
Edition description: | 3rd ed. |
Pages: | 525 |
Product dimensions: | 7.38(w) x 9.25(h) x 0.70(d) |
About the Author
Christian Bauer, Gavin King, and Gary Gregory are the authors of Java Persistence with Hibernate, Second Edition, on which this book is based.
Table of Contents
Preface xiii
Acknowledgments xv
About this hook xvi
About the author xix
About the cover illustration xx
Part 1 JUnit 1
1 JUnit jump-start 3
1.1 Proving that a program works 4
1.2 Starting from scratch 6
Understanding unit testing frameworks 8
Adding unit tests 9
1.3 Setting up JUnit 9
1.4 Testing with JUnit 12
2 Exploring core JUnit 15
2.1 Core annotations 16
The @DisplayName annotation 19
The @Disabled annotation 20
2.2 Nested tests 21
2.3 Tagged tests 23
2.4 Assertions 25
2.5 Assumptions 29
2.6 Dependency injection in JUnit 5 31
TestInfoParameterResolver 31
TestReporterParameterResolver 32
RepetitionInfoParameterResolver 33
2.7 Repeated tests 33
2.8 Parameterized tests 35
2.9 Dynamic tests 38
2.10 Using Hamcrest matchers 40
3 JUnit architecture 47
3.1 The concept and importance of software architecture 48
Story 1 The telephone directories 48
Story 2 The sneakers manufacturer 49
3.2 The JUnit 4 architecture 49
JUnit 4 modularity 50
JUnit 4 runners 50
JUnit 4 rules 52
Shortcomings of the JUnit 4 architecture 58
3.3 The JUnit 5 architecture 58
JUnit 5 modularity 58
JUnit Platform 59
JUnit Jupiter 60
JUnit Vintage 60
The big picture of the JUnit 5 architecture 60
4 Migrating from JUnit 4 to JUnit 5 63
4.1 Migration steps between JUnit 4 and JUnit 5 64
4.2 Needed dependencies 65
4.3 Annotations, classes, and methods 67
Equivalent annotations, classes, and methods 67
Categories vs. tags 71
Migrating Hamcrest matcher functionality 77
Rules vs. the extension model 78
Custom rules 82
5 Software testing principles 87
5.1 The need for unit tests 88
Allowing greater test coverage 88
Increasing team productivity 88
Detecting regressions and limiting debugging 88
Refactoring with confidence 89
Improving implementation 90
Documenting expected behavior 90
Enabling code, coverage and other metrics 91
5.2 Test types 92
Unit testing 92
Integration software testing 93
System software testing 93
Acceptance software testing 94
5.3 Black-box vs. white-box testing 94
Black-box testing 95
White-box testing 95
Pros and cons 96
Part 2 Different Testing Strategies 99
6 Test quality 101
6.1 Measuring test coverage 102
Introduction to test coverage 102
Tools for measuring code coverage 103
6.2 Writing testable code 108
Understanding that public APIs are contracts 108
Reducing dependencies 108
Creating simple constructors 110
Following the Law of Demeter (Principle of Least Knowledge) 110
Avoiding hidden dependencies and global, state 112
Favoring generic methods 113
Favoring composition over inheritance 114
Favoring polymorphism over conditionals 114
6.3 Test-driven development 116
Adapting the development cycle 116
Doing the TDD two-step 117
6.4 Behavior-driven development 117
6.5 Mutation testing 118
6.6 Testing in the development cycle 119
7 Coarse-grained testing with stubs 123
7.1 Introducing stubs 124
7.2 Stubbing an HTTP connection 125
Choosing a stubbing solution 128
Using Jetty as an embedded server 128
7.3 Stubbing the web server resources 130
Setting up the first stub test 130
Reviewing the first stub test 133
7.4 Stubbing the connection 134
Producing a custom URL protocol handler 134
Creating a JDK HttpURLConnection stub 135
Running the test 136
8 Testing with mock objects 138
8.1 Introducing mock objects 139
8.2 Unit testing with mock objects 139
8.3 Refactoring with mock objects 143
Refactoring example 144
Refactoring considerations 145
8.4 Mocking an HTTP connection 147
Defining the mock objects 147
Testing a sample method 148
Try #1 Easy refactoring technique 149
Try #2 Refactoring by using a class factory 151
8.5 Using mocks as Trojan horses 154
8.6 Introducing mock frameworks 156
Using EasyMock 157
Using JMock 162
Using Mockito 166
9 In-container testing 170
9.1 Limitations of standard unit testing 171
9.2 The mock-objects solution 172
9.3 The step to in-container testing 174
Implementation strategies 174
In-container testing frameworks 175
9.4 Comparing stubs, mock objects, and in-container testing 175
Stubs evaluation 175
Mock-objects evaluation 176
In-container testing evaluation 177
9.5 Testing with Arquillian 178
Part 3 Working with JUnit 5 and other Tools 187
10 Running JUnit tests from Maven 3 189
10.1 Setting up a Maven project 190
10.2 Using the Maven plugins 193
Maven compiler plugin 194
Maven Surefire plugin 195
Generating HTML JUnit reports with Maven 197
10.3 Putting it all together 198
10.4 Maven challenges 203
11 Running JUnit tests from Gradle 6 205
11.1 Introducing Gradle 206
11.2 Setting up a Gradle project 207
11.3 Using Gradle plugins 212
11.4 Creating a Gradle project from scratch and testing it with JUnit 5 213
11.5 Comparing Gradle and Maven 218
12 JUnit 5 IDE support 219
12.1 Using JUnit 5 with IntelliJ IDEA 220
12.2 Using JUnit 5 with Eclipse 226
12.3 Using JUnit 5 with NetBeans 232
12.4 Comparing JUnit 5 usage in IntelliJ, Eclipse, and NetBeans 238
13 Continuous integration with JUnit 5 240
13.1 Continuous integration testing 241
13.2 Introducing Jenkins 243
13.3 Practicing CI on a team 246
13.4 Configuring Jenkins 251
13.5 Working on tasks in a CI environment 255
Part 4 Working with Modern Frameworks and JUnit 5 261
14 JUnit 5 extension model 263
14.1 Introducing the JUnit 5 extension model 264
14.2 Creating a JUnit 5 extension 264
14.3 Writing JUnit 5 tests using the available extension points 268
Persisting passengers to a database 268
Checking the uniqueness of passengers 277
15 Presentation-layer testing 281
15.1 Choosing a testing framework 282
15.2 Introducing HtmlUnit 282
A live example 282
15.3 Writing HtmlUnit tests 284
HTML assertions 284
Testing for a specific web browser 285
Testing more than one web browser 285
Creating standalone tests 286
Testing forms 288
Testing JavaScript 290
15.4 Introducing Selenium 294
15.5 Writing Selenium tests 295
Testing for a specific web browser 297
Testing navigation using a web browser 298
Testing more than one web browser 299
Testing Google search and navigation using different web browsers 301
Testing website authentication 303
15.6 HtmlUnit vs. Selenium 309
16 Testing Spring applications 311
16.1 Introducing the Spring Framework 311
16.2 Introducing dependency injection 312
16.3 Using and testing a Spring application 317
Creating the Spring context programmatically 317
Using the Spring TestContext framework 321
16.4 Using SpringExtension for JUnit Jupiter 322
16.5 Adding a new feature and testing it with JUnit 5 325
17 Testing Spring Boot applications 333
17.1 Introducing Spring Boot 334
17.2 Creating a project with Spring Initializr 334
17.3 Moving the Spring application to Spring Boot 337
17.4 Implementing a test-specific configuration for Spring Boot 339
17.5 Adding and testing a new feature in the Spring Boot application 342
8 Testing a REST API 348
18.1 Introducing REST applications 349
18.2 Creating a RESTful API to manage one entity 350
18.3 Creating a RESTful API to manage two related entities 357
18.4 Testing the RESTful API 364
19 Testing database applications 369
19.1 The database unit testing impedance mismatch 370
Unit tests must exercise code in isolation 370
Unit tests must be easy to write and run 370
Unit tests must be fast to run 371
19.2 Testing a JDBC application 372
19.3 Testing a Spring JDBC application 381
19.4 Testing a Hibernate application 388
19.5 Testing a Spring Hibernate application 393
19.6 Comparing the approaches for testing database applications 400
Part 5 Developing applications with JUnit 5 403
20 Test-driven development with JUnit 5 405
20.1 TDD main concepts 406
20.2 The flight-management application 407
20.3 Preparing the flight-management application for TDD 412
20.4 Refactoring the flight-management application 416
20.5 Introducing new features using TDD 420
Adding a premium flight 420
Adding a passenger only once 429
21 Behavior-driven development with JUnit 5 434
21.1 Introducing behavior-driven development 435
Introducing a new feature 435
From requirements analysis to acceptance criteria 436
BDD benefits and challenges 437
21.2 Working BDD style with Cucumber and JUnit 5 437
Introducing Cucumber 438
Moving a TDD feature to Cucumber 439
Adding a new feature with the help of Cucumber 447
21.3 Working BDD style with JBehave and JUnit 5 454
Introducing JBehave 455
Moving a TDD feature to JBehave 455
Adding a new feature with the help of JBehave 461
21.4 Comparing Cucumber and JBehave 469
22 Implementing a test pyramid strategy with JUnit 5 471
22.1 Software testing levels 472
22.2 Unit testing: Basic components working in isolation 473
22.3 Integration testing: Units combined into a group 483
22.4 System testing: Looking at the complete software 491
Testing with a mock external dependency 491
Testing with a partially implemented external dependency 495
Testing with the fully implemented external dependency 498
22.5 Acceptance testing: Compliance with business requirements 501
Appendix A Maven 513
Appendix B Gradle 520
Appendix C IDEs 525
Appendix D Jenkins 528
Index 531