Book Image

MOCKITO COOKBOOK

By : Grzejszczak
Book Image

MOCKITO COOKBOOK

By: Grzejszczak

Overview of this book

This is a focused guide with lots of practical recipes with presentations of business issues and presentation of the whole test of the system. This book shows the use of Mockito's popular unit testing frameworks such as JUnit, PowerMock, TestNG, and so on. If you are a software developer with no testing experience (especially with Mockito) and you want to start using Mockito in the most efficient way then this book is for you. This book assumes that you have a good knowledge level and understanding of Java-based unit testing frameworks.
Table of Contents (12 chapters)
11
Index

Mockito versus JMockit


In this recipe, we will write a simple test using JMockit that verifies the behavior of the system under test when an exception is thrown.

Getting ready

In order to profit from JMockit, you need to add it to your classpath. The following is the JMockit configuration for Gradle:

testCompile 'com.googlecode.jmockit:jmockit:1.7'

The following is how you can add JMockit to your classpath using Maven:

<dependency>
  <groupId>com.googlecode.jmockit</groupId>
  <artifactId>jmockit</artifactId>
  <version>1.7</version>
  <scope>test</scope>
</dependency>            

Note

If you do not use @RunWith(JMockit.class), then you need to define the JMockit dependency before the JUnit one! Please refer to http://jmockit.googlecode.com/svn/trunk/www/gettingStarted.html for more information.

Let's assume that our system under test is the tax transferring system for a given person, as shown in the following code:

public class TaxTransferer...