What is Mockito?

The open source testing framework for Java

What is Mockito?

Mockito is a play on the words "mocking" and "mojito". While Java might not have anything to do with a beverage - other than Java coffee, that is - the concept of Mocking in Java is the method of testing each unit in a program individually, or in isolation.

It is called such because it helps the user to test each unit in a large program in isolation by creating mock classes. 

As the name suggests, a mock object is an object that is created in the testing environment in lieu of an actual object that would be there when the full program is run.

Why Do We Need to Mock?

When programming a Java application, it is possible that some objects may not be created, or some objects may be obtained from a service or API.

Despite the absence of these objects, mocking allows testing by creation of a mock object in order to test the unit.

This implies that any unit can be tested independently, without having to rely on external services or other objects.

With the use of Mockito and thereby, mocking, it is possible to test just the unit that is required rather than testing the whole system.

Benefits of Mockito

  • Automatic creation of mock objects
  • Gives support for return values
  • Gives support for exceptions
  • Creates mocks using annotations
  • Code will not break when methods or interfaces are renamed

Comments

Popular Posts