Home » Tag Archives: Mockito (page 2)

Tag Archives: Mockito

Writing Clean Tests – Trouble in Paradise

If our code has obvious faults, we are very motivated to improve it. However, at some point we decide that our code is “good enough” and move on. Typically this happens when we think that the benefits of improving our existing code are smaller than the required work. Of course, if we underestimate our return of investment, we can make ...

Read More »

Book Review: Mockito Essentials

The subtitle of Sujoy Acharya‘s Mockito Essentials (Packt Publishing, October 2014) is: “A practical guide to get you up and running with unit testing using Mockito.” The Preface and seven chapters in Mockito Essentials span approximately 190 substantive pages. Preface In the Preface, author Sujoy Acharya writes that Mockito Essentials “is an advanced-level guide that will help software developers to ...

Read More »

Unit Testing exercise with FizzBuzz and Mockito

I sometimes use FizzBuzz to demonstrate the basics of unit testing to newbies. Although FizzBuzz is really simple problem, it can also be used to demonstrate more advanced unit testing techniques like mocking. The FizzBuzz Kata: “Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for ...

Read More »

Mockito 101

Mockito is a mocking framework that lets you write beatiful tests with clean and simple API. It biases toward minimal specifications, makes different behaviors look different, and displays clear error messages. Creating Mocks To create a mock using Mockito, simply annotate mocks with @Mock and call MockitoAnnotations.initMocks(this).       import org.mockito.Mock; import org.mockito.MockitoAnnotations; public class FooClassTest { @Mock mockFoo; ...

Read More »

Writing Clean Tests – Divide and Conquer

A good unit test should fail for only one reason. This means that a proper unit test tests only one logical concept. If we want to write clean tests, we have to identify those logical concepts, and write only one test case per logical concept. This blog post describes how we can identify the logical concepts found from our tests, ...

Read More »

Writing Clean Tests – Beware of Magic

It is pretty hard to figure out a good definition for clean code because everyone of us has our own definition for the word clean. However, there is one definition which seems to be universal: Clean code is easy to read. This might come as a surprise to some of you, but I think that this definition applies to test ...

Read More »

What are Mockito Extra Interfaces?

Mockito is my favored little helper if it comes down to write light weight JUnit tests. It is very useful to replace the ‘real’ dependencies of a unit under test easily by mocks if necessary. In particular when working on the borderline to framework APIs such dependencies can otherwise be very expensive to setup. But sometimes the situation is a ...

Read More »

Getting started with Mocking in Java using Mockito

We all write unit tests but the challenge we face at times is that the unit under test might be dependent on other components. And configuring other components for unit testing is definitely an overkill. Instead we can make use of Mocks in place of the other components and continue with the unit testing. To show how one can use ...

Read More »

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns