Java testing with Mockito and the Hamcrest matchers
The only way to be sure your code is working properly is to test it. Test-driven development (TDD) techniques not only help ensure that your code is correct but also make it much easier to make changes in the future. The JUnit testing framework has been around for a generation and is accepted as the de facto standard for unit testing, but by itself it can only do so much.
Join Ken Kousen for a dive into Java testing using Hamcrest matchers, which make it possible to write cleaner tests that are easier to understand and interpret, and the Mockito testing framework, which lets you create mock objects that isolate potential problems in your code. You'll learn to write fast, effective unit tests that also provide executable documentation for your system.
What you'll learn-and how you can apply it
By the end of this live online course, you’ll understand:
- The limitations of existing unit testing approaches
- How the Hamcrest library uses "syntactic sugar" to make tests easier to read and understand
- How mock objects help isolate potential problems quickly and easily
And you’ll be able to:
- Write and revise unit tests with the latest features of the Hamcrest libraries
- Create mock objects that let you focus on the components of the system you're actually interested in, which helps keep you from being distracted by external behavior
This training course is for you because...
- You're a Java developer who wants to verify that your code is behaving properly.
- You want to create a powerful suite of tests that gives you the flexibility to make significant upgrades over time. If you modify the system and a test fails, you want to know exactly where and why.
- You want to make your tests more readable and easier to understand.
- A working knowledge of Java
- Experience with the JUnit testing framework
Beginning Java, 2nd edition (Learning Path)
Next Generation Java testing with JUnit 5 (live online training)
Unit Testing with the Spock Framework (live online training)
About your instructor
Ken Kousen is the author of the books Modern Java Recipes (O'Reilly), Gradle Recipes for Android (O’Reilly) and Making Java Groovy (Manning), as well as O’Reilly video courses in Android, Groovy, Gradle, Advanced Java, and Spring. He is a regular speaker on the No Fluff, Just Stuff conference tour and a 2013 and 2016 JavaOne Rock Star, and has spoken at conferences all over the world. Through his company, Kousen I.T., Inc., he has taught software development training courses to thousands of students.
The timeframes are only estimates and may vary according to how the class is progressing
JUnit testing (35 minutes)
- Lecture and hands-on exercises: Adding JUnit to a Java system; the basics of unit tests; assertions; pre- and postconditions; new features in JUnit 5; executing a test suite; detecting and fixing problems
The Hamcrest matchers (40 minutes)
- Lecture and hands-on exercises: Adding the Hamcrest dependency; using static imports; overview of the Hamcrest matchers; using Hamcrest matchers for lists; matching bean properties; string matchers; writing custom matchers; combining matchers
Break (10 minutes)
The Mockito framework (40 minutes)
- Lecture and hands-on exercises: Understanding stubs, mocks, fakes, and spies; adding the Mockito dependency; stubbing method calls; throwing exceptions; using argument matchers; comparison matchers; compound value matchers; verifying method calls
Break (10 minutes)
Advanced Mockito usage (40 minutes)
- Lecture and hands-on exercises: Working with void methods; Mockito callback methods; generic collection arguments; verifying invocation order; creating test spies; resetting mock objects; behavior-driven development (BDD) with Mockito
Wrap-up and Q&A (5 minutes)