Chapter 10. Mocking Libraries Comparison

In this chapter, we will cover the following recipes:

  • Mockito versus EasyMock
  • Mockito versus JMockit
  • Mockito versus jMock
  • Mockito versus Spock

Introduction

In this chapter, we will take a look at other mocking frameworks that are quite well known in the Java world. The idea of this chapter is not to state whether one mocking framework is better than Mockito, but to point out differences in both their syntax and approach.

Remember that the examples presented in this chapter are very simple and do not show all of the possible ways of using the mocking frameworks, since you could write books about any of them.

Before moving forward, it's worth mentioning the difference between a strict mock and a non-strict one:

  • Strict mock: This is a mock that will fail the moment anything differs from the expectations. In other words, if you expect your mock to call some methods and that doesn't happen, then your test will fail.
  • Non-strict mock: This is a mock that will ignore any methods that were expected and were not executed. Your test won't fail even when an unexpected method is called. Mockito's mocks are non-strict.

It's important to understand the difference because EasyMock, JMockit, and JMock allow you to create either of those mocks.

Let's come back to the chapter's structure. We will start off by taking a look at EasyMock, which is Mockito's predecessor (in fact, Mockito began as the EasyMock's fork). Next, we will take a look at JMockit and JMock, which are similar to some extent. Finally, we will see how you can do things the Groovy way using Spock.

In all cases, we will use the tax transferring system which will throw an exception during the transfer of tax.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.254.61