Mockito Junit Tutorial to Create Your First Code in Mockito

The key to simple and quick unit tests is Mocking. And, when it comes to mocking, there is no better tool than Mockito. This ‘Mockito JUnit Tutorial’ will enable you to learn everything from a basic unit test to your first Mockito code. 

What Is Unit Testing?

Unit Testing or Component Testing is a software testing technique in which a single software component is tested. The developers perform the testing during the development phase to ensure the successful working of each unit.

Unit_Testing_Mockito

Unit Tests are a set of automated tests that make the identification and fixation of bugs in the early SDLC stages. You need to perform the testing before Integration Testing, and this increases the speed of the entire development process. 

What Is Mocking?

Mocking refers to the development of objects which are a mock or clone of real objects. In the technique, it uses mock objects instead of real objects for testing. As a result, a mock object provides a specific mock output for each mock input passed. 

Mock objects give a particular output for each particular input. Mockito is the most popular framework used for Mocking.

What Is Mockito?

Mockito is a Java-based framework used for unit testing of Java applications. This mocking framework helps in the development of testable applications.

Mockito_Junit_Tutorial.

You use Java Reflection API internally to generate mock objects. Mockito is used to simplify test development by mocking external dependencies and using them in the code.

Two important prerequisites are to be kept in mind before starting with learning Mockito are:

  • Java: Hands-on experience in Java programming.
  • JUnit: Basic understanding of the JUnit Framework.

Benefits of Mockito

The benefits or advantages of Mockito are explained in the following points:

  • No handwriting - The developers do not need to write their Mock codes.
  • Return values - Mockito supports the return values.
  • Safe Refactoring - Even if an interface method is renamed or the parameters are reordered, the test codes created as Mocks will not break.

Benefits_Of_Mockito

  • Exception support - Mockito enables exceptions.

  • Annotation support - Mockito supports the creation of Mocks with annotation.
  • Order Support - Mockito allows checking on the order of method calls.

Demo: First Mockito Code

Before starting the demo, there are certain prerequisites for creating your first Mockito code:

1. Java

2. Eclipse

3. JUnit

4. Maven Dependencies

  • First, you will have to create a java file.
  • Right-click on the main JAR file → New → Class 

First_Java_Class.

  • Now, name the Java file to be “Stock”
  • Then create a stock class.
  • You can see basic setters and getters in the code like stockID, setstockID, etc.

Stock_Class_With_Functions

  • Now create an interface:
    • Right-click on the main JAR file → New → Interface
  • Next, name the interface to be “StockService”
  • After you create the StockService interface, create a portfolio Java class.

Stock_Service_Interface.

  • Right-click on the main JAR file → New → Class 
  • Now, name the Java file to be “Portfolio”

Portfolio_Java_Class

  • Then in the portfolio.java class:
  • Since there are list operations being used, import java.util for that
  • In this class, it is evident that the StockService interface is also being called to fetch the details of the main class

Now, when these source codes are ready, the next step is to work on the test cases. 

  • For test cases:
  • Right-click on the test JAR file → New → Class
  • Let’s name the Java file to be “PortfolioTester”

PortfolioSetter_Mockito.

Now, several Mockito JAR files will be used. As Maven is already installed in the system, dependencies are present in the pom.xml file and Maven Dependencies folder. Hence, there are no errors whenever any imports are being done.

Maven_Dependencies_Mockito

In the PortfolioTester, there are several major steps that you’ve to perform.

  • First, you have to call the StockService 
  • Then in the code, you have to provide some dummy data
  • In the code, you need a function called tester.setUp(), followed by testMarketValue()

Dummy_Data_mockito

The next step is to run the code.

  • In the PortfolioTester.java:
    • Right-click on the screen

Running_Mockito_Code

  • In the dropdown select ‘Run As’ and select ‘Java Application’
  • Then the “Pass” result is visible on the screen

Pass_Output_Mockito

This is how real objects can be mocked. Any input could be put or edited and the output will appear accordingly. 

Conclusion

By now, you have gained a lot of knowledge regarding Mockito and its working. At the beginning of the Mockito JUnit tutorial, you understood what Unit Testing is all about, followed by the process of mocking. After introducing mocking, you saw the most popular mocking framework that is Mockito. While at it, you also learned the benefits of Mockito and how to run your first code in Mockito with a hands-on demo.

For more information, go ahead and check out this video on Mockito JUnit Tutorial.

To get more training on Mockito JUnit, you can pursue Simplilearn’s DevOps Engineer Master’s Program. This comprehensive program provides you with all the skills you’ll need and access to the most widely used DevOps tools through a combination of live, instructor-led classes, online self-paced learning, and hands-on projects. If you’re just beginning or want to take your career to the next level, Simplilearn has something for all aspiring DevOps professionals. To learn more about what we’re up to, check out additional resources here.

If you have any questions on the topic, do place them below in the comment section of this "Mockito JUnit Tutorial" article. We'll get our subject matter experts to answer them for you as soon as possible.

Happy learning!

About the Author

Ishan GabaIshan Gaba

Ishan Gaba is a Research Analyst at Simplilearn. He is proficient in Java Programming, Data Structures, and Project Management. Graduated in Information Technology, Ishan is also passionate about writing and traveling.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.