You are previewing Mockito for Spring.
O'Reilly logo
Mockito for Spring

Book Description

Learn all you need to know about the Spring Framework and how to unit test your projects with Mockito

In Detail

Mockito is a testing framework that creates external dependencies; this process is called mocking. This book will provide you with the skills you need to successfully build and maintain meaningful unit and integration test cases for Spring Framework code. This book starts with a brief introduction to the Spring Framework, including its components and architecture.

Packed with real-world examples, the book covers how to make Spring code testable and mock container and external dependencies using Mockito. You then get a walkthrough of the steps that will help you in applying Spring 4 Framework features and advanced Spring testing module features in the real world. By the end of the book, you will be a competent software tester with Spring and Mockito.

What You Will Learn

  • Uncover the architecture of the Spring testing framework and write integration tests

  • Explore test doubles and work with dummy, spy, fake, stub, and mock objects

  • Mock Spring container dependencies using Mockito

  • Refresh your Spring Framework knowledge

  • Learn about the advanced Spring 4 Framework features

  • Work with the REST controller to build and unit test RESTful web services

  • Cache data with Spring and EhCache

  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Mockito for Spring
      1. Table of Contents
      2. Mockito for Spring
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Familiar with the Spring Framework
        1. Getting started with Spring
        2. Exploring the Spring architecture
          1. The core container
          2. The AOP module
          3. The instrumentation module
          4. The messaging module
          5. The data access module
          6. The web layer
          7. The test module
        3. Learning the Inversion of Control
        4. Printing Hello World
        5. Examining life cycle messages
        6. Working with autowiring and annotations
        7. Working with aspects
        8. Exploring Spring JDBC
        9. Handling a transaction with Spring
          1. Working with declarative Spring transaction
          2. Exploring transaction attributes
          3. Using the @Transactional annotation
          4. Working with a programmatic Spring transaction
            1. Using TransactionTemplate
            2. Using PlatformTransactionManager
        10. Building an MVC application with Spring
        11. Summary
      9. 2. Working with JUnit and Mockito
        1. Learning unit testing
        2. Working with the JUnit framework
          1. Configuring Eclipse
          2. Examining annotations
          3. Verifying an expectation with an assertion
          4. Examining exception handling
          5. Working with the @RunWith annotation
          6. Working with test suites
          7. Working with assertThat
            1. Exploring equalTo, is, and not
            2. Exploring compound matchers – either, both, anyOf, and allOf
            3. Exploring collection matchers – hasItem and hasItems
            4. Working with string matchers – startsWith, endsWith, and containsString
            5. Exploring custom matchers
        3. Working with Mockito
          1. Learning the significance of Mockito
        4. Exploring Mockito
          1. Configuring Mockito
          2. Mocking in action
            1. Mocking objects
            2. Stubbing methods
              1. Verifying in depth
              2. Verifying zero and no more interactions
            3. Throwing exceptions
        5. Summary
      10. 3. Working with Spring Tests
        1. Exploring the TestContext framework
          1. Writing a custom TestExecutionListener interface
        2. Configuring Spring profiles
        3. Mocking an environment
        4. Mocking the JNDI lookup
        5. Using ReflectionTestUtils
        6. Working with annotations
        7. Testing Spring MVC
        8. Mocking the servlet container with MockMvc
        9. Handling transactions in Spring tests
        10. Summary
      11. 4. Resolving Out-of-container Dependencies with Mockito
        1. Unit testing the web layer
        2. Unit testing the service layer
        3. Unit testing the data access layer
        4. Summary
      12. 5. Time Travelling with Spring
        1. Discovering the new Spring release
        2. Working with asynchronous tasks
        3. Exploring @RestController
        4. Learning AsyncRestTemplate
        5. Caching objects
        6. Summary
      13. Index