Book Image

Mockito Essentials

By : Sujoy Acharya
Book Image

Mockito Essentials

By: Sujoy Acharya

Overview of this book

Table of Contents (14 chapters)

Preface

We can acquire knowledge in different ways. On one side is theory, and on the other side is the application of theory. Both are important and both make us better. Theoretical knowledge can provide us with a deep understanding of the concept through the experiences of others, but a practical application can give us a deep understanding through the reality of life and the act of doing.

I was looking for a Mockito framework guide that could teach me the practical application of the framework, but I didn't find any book or article. Then, I decided to start writing a book that can focus on both the theoretical aspect and the practical application, so that readers can get a deep understanding of the concepts through the act of doing.

This book is an advanced-level guide that will help software developers to get complete expertise in unit testing using Mockito as the mocking framework. The focus of the book is to provide readers with comprehensive details on how effectively Mockito can be used for mocking external dependencies in Java application, web application, legacy code, GWT, and SOA.

Armed with the knowledge of advanced JUnit concepts and mocking framework essentials, you will be pleasantly surprised at how quickly and easily you can write high-quality, clean, readable, testable, maintainable, and extensible code.

What this book covers

Chapter 1, Exploring Test Doubles, covers the concept of automated unit tests, talks about the characteristics of a good unit test, and explores the test's doubles. It provides examples of dummy objects, fake objects, stubs, mock objects, and spies.

Chapter 2, Socializing with Mockito, focuses on getting the reader quickly started with the Mockito overview, unit test qualities, and the significance of Mockito in unit testing. It also explains and provides examples of stubbing, answering, throwing exceptions, argument matchers, and method call verification. The Mockito architecture is also uncovered.

Chapter 3, Accelerating Mockito, illustrates advanced Mockito framework topics, such as working with void methods, throwing exceptions from void methods, writing callbacks for void methods, returning values using doReturn, void method chaining, calling original methods, Mockito annotations, verifying arguments using an argument captor, verifying an invocation order, spying objects using spy, changing default Mockito settings, resetting mock objects, inline stubbing, and mock details.

Chapter 4, Behavior-driven Development with Mockito, unfolds the BDD concepts, BDD examples, and writing BDD style tests with Mockito.

Chapter 5, Unit Testing the Legacy Code with Mockito, explores legacy code, testing impediments, design for testability, and unit testing the legacy code with Mockito and PowerMock. By the end of this chapter, the reader will be able to write JUnit tests for a legacy code with Mockito and PowerMock, refactor the legacy code to make it unit testable, and design code to bypass the testing impediments.

Chapter 6, Developing SOA with Mockito, deals with web services, explores SOAP and RESTful web services with examples, and helps us to write JUnit tests for the web services with Mockito to mock out the web service framework dependencies.

Chapter 7, Unit Testing GWT Code with Mockito, provides an overview of Ajax/GWT, explains the MVP pattern and loose coupling, and provides examples and strategies to mock GWT widgets using Mockito.

What you need for this book

You will need the following software to be installed before running the examples in this book:

Who this book is for

This book is for advanced to novice level software testers/developers using Mockito in the JUnit framework, with a reasonable knowledge level and understanding of unit testing elements and applications.

It is ideal for developers who have some experience in Java application development as well as some basic knowledge of JUnit testing, but it covers the basic fundamentals of JUnit testing and the Mockito framework to get you acquainted with these concepts before using them.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Tests should be readable and expressive; for example, a test that verifies unauthorized access can be written as testUnauthorizedAccess() or rather when_an_unauthorized_user_accesses_the_system_then_raises_secuirty_error()."

A block of code is set as follows:

@Test
  public void currencyRoundsOff() throws Exception {
    assertNotNull(CurrencyFormatter.format(100.999));
    assertTrue(CurrencyFormatter.format(100.999).contains("$"));
    assertEquals("$101.00", CurrencyFormatter.format(100.999));
  }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

public class LocaleTest {
  private Locale defaultLocale;
  @Before
  public void setUp() {
    defaultLocale = Locale.getDefault();
    Locale.setDefault(Locale.GERMANY);
  }
  @After
  public void restore() {
    Locale.setDefault(defaultLocale);
  }
  @Test
  public void currencyRoundsOff() throws Exception {
    assertEquals("$101.00", CurrencyFormatter.format(100.999));
  }
}

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Go to the Libraries tab in the project's build path."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

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.

Downloading the color images of this book

We also provide you a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/3605OS_Graphics.pdf.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.