Mockito
Presented by : Shahrzad Fereidani
Professor : Dr. Ajoudanian
Introduction to Mockito
What is Mockito?
- Mockito is a popular open-source Java mocking framework.
- It allows you to create and configure mock objects for testing purposes.
- Developed to be simple, intuitive, and easy to use.
- Helps in isolating the unit of code being tested by simulating the behavior of
dependencies.
Why Use Mockito?
- Simplifies the creation of mock objects.
- Provides readable and maintainable code for unit tests.
- Eliminates the need for complex setup code.
- Helps achieve high test coverage by isolating and testing individual components.
Key Concepts of Mockito
Mocks:
- Dummy objects that simulate the behavior of real objects.
- Used to isolate the unit of code under test.
- Helps in focusing tests on the behavior of the unit under test, not its dependencies.
Key Concepts of Mockito
Stubbing:
- Configuring mock objects to return specific values when certain methods are called.
- Used to define the behavior of a mock object.
- Helps in testing different scenarios by controlling the return values and exceptions.
Key Concepts of Mockito
Verification:
- Ensuring that certain methods were called on mock objects.
- Used to confirm that the unit of code under test interacted with its dependencies as
expected.
- Helps in validating the correctness of the code logic.
Setting Up Mockito
Add Mockito to your project using Maven or Gradle.
-Example (Maven): org.mockito mockito-core 4.x.x test
- Example (Gradle): testImplementation 'org.mockito:mockito-core:4.x.x
Creating Mocks
- Using @Mock Annotation: @Mock MyService myService;
- Using Mockito.mock(): MyService myService = Mockito.mock(MyService.class);
Stubbing Methods
- Basic Stubbing: when(myService.performOperation()).thenReturn(expectedResult);
- Throwing Exceptions: when(myService.performOperation()).thenThrow(new
RuntimeException());
Verifying Behavior
- Verifying Method Calls: verify(myService).performOperation();
- Verifying Call Counts: verify(myService, times(2)).performOperation();
Practical Example - Testing a Service
Scenario:
- Testing a UserService that depends on a UserRepository.
Code Example:
Advanced Features
Argument Captors:
ArgumentCaptor captor = ArgumentCaptor.forClass(User.class);
verify(userRepository).save(captor.capture());
User capturedUser = captor.getValue();
Spies:
List list = new ArrayList<>();
List spyList = spy(list);
when(spyList.size()).thenReturn(100);
Best Practices
Keep Tests Simple:
- Focus on one behavior per test.
Use Annotations:
- @Mock, @InjectMocks, @Captor for readability.
Avoid Over-Mocking:
- Mock only what you need.
Mockito Alternatives
● EasyMock
● JMock
● PowerMock
Conclusion
● Mockito is a powerful tool for unit testing in Java.
● It helps create clean, maintainable tests by isolating components.
References and Further Reading
- Mockito Documentation: https://site.mockito.org/ - Books:
- "Mockito Made Clear" by Sujoy Acharya
- "Practical Unit Testing with JUnit and Mockito" by Tomek Kaczanowski
- Online Tutorials: - Baeldung Mockito Guide: https://www.baeldung.com/mockito-series
Thank You!

Mockito a software testing project a.pdf

  • 1.
    Mockito Presented by :Shahrzad Fereidani Professor : Dr. Ajoudanian
  • 2.
  • 3.
    What is Mockito? -Mockito is a popular open-source Java mocking framework. - It allows you to create and configure mock objects for testing purposes. - Developed to be simple, intuitive, and easy to use. - Helps in isolating the unit of code being tested by simulating the behavior of dependencies.
  • 4.
    Why Use Mockito? -Simplifies the creation of mock objects. - Provides readable and maintainable code for unit tests. - Eliminates the need for complex setup code. - Helps achieve high test coverage by isolating and testing individual components.
  • 5.
    Key Concepts ofMockito Mocks: - Dummy objects that simulate the behavior of real objects. - Used to isolate the unit of code under test. - Helps in focusing tests on the behavior of the unit under test, not its dependencies.
  • 6.
    Key Concepts ofMockito Stubbing: - Configuring mock objects to return specific values when certain methods are called. - Used to define the behavior of a mock object. - Helps in testing different scenarios by controlling the return values and exceptions.
  • 7.
    Key Concepts ofMockito Verification: - Ensuring that certain methods were called on mock objects. - Used to confirm that the unit of code under test interacted with its dependencies as expected. - Helps in validating the correctness of the code logic.
  • 8.
    Setting Up Mockito AddMockito to your project using Maven or Gradle. -Example (Maven): org.mockito mockito-core 4.x.x test - Example (Gradle): testImplementation 'org.mockito:mockito-core:4.x.x
  • 9.
    Creating Mocks - Using@Mock Annotation: @Mock MyService myService; - Using Mockito.mock(): MyService myService = Mockito.mock(MyService.class);
  • 10.
    Stubbing Methods - BasicStubbing: when(myService.performOperation()).thenReturn(expectedResult); - Throwing Exceptions: when(myService.performOperation()).thenThrow(new RuntimeException());
  • 11.
    Verifying Behavior - VerifyingMethod Calls: verify(myService).performOperation(); - Verifying Call Counts: verify(myService, times(2)).performOperation();
  • 12.
    Practical Example -Testing a Service Scenario: - Testing a UserService that depends on a UserRepository. Code Example:
  • 13.
    Advanced Features Argument Captors: ArgumentCaptorcaptor = ArgumentCaptor.forClass(User.class); verify(userRepository).save(captor.capture()); User capturedUser = captor.getValue(); Spies: List list = new ArrayList<>(); List spyList = spy(list); when(spyList.size()).thenReturn(100);
  • 14.
    Best Practices Keep TestsSimple: - Focus on one behavior per test. Use Annotations: - @Mock, @InjectMocks, @Captor for readability. Avoid Over-Mocking: - Mock only what you need.
  • 15.
  • 16.
    Conclusion ● Mockito isa powerful tool for unit testing in Java. ● It helps create clean, maintainable tests by isolating components.
  • 17.
    References and FurtherReading - Mockito Documentation: https://site.mockito.org/ - Books: - "Mockito Made Clear" by Sujoy Acharya - "Practical Unit Testing with JUnit and Mockito" by Tomek Kaczanowski - Online Tutorials: - Baeldung Mockito Guide: https://www.baeldung.com/mockito-series
  • 18.