Mockito for beginners

Mockito for beginners

Mockito for beginners:

Mockito is an open source testing framework, and it is a widely used for junit testing with mocking beans/repositories in frameworks such as spring mvc & spring boot. The framework allows the creation of test double objects in automated unit tests for the purpose of test-driven development or behavior-driven development. The framework’s name and logo are a play on mojitos, a type of drink.

How to Mock void method ?

doNothing().when(couponRepository).deleteAll(Mockito.any());

Note: deleteAll is a void method.

 

How to Mock public methods of a same class ?

NgDeveloper ngDeveloperSpy = Mockito.spy(new NgDeveloper());
when(ngDeveloperSpy.getAuthor()).thenReturn("Mirthbees");

Note:¬†getAuthor() is the public method which we need to mock, so we have created a spy of a class then called getAuthor() method and mocked the response as “Mirthbees” using when and thenReturn.

772 total views, 4 views today

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.