Mastering Mockito in JUnit Testing: Simplifying Test Development

Posted on by By admin, in Software Testing | 0

Mockito is junit testing framework , which actually mock the behaviour of methods.

It simplify the development of tests of classes with dependencies.

I hope you are familiar with junit .

A mock object is dummy object of classes and interface.

Mastering Mockito in JUnit Testing: Simplifying Test Development

How to use when-then and when-thenThrow

When we want mock the object which returns any objects or any datatype values

We can use this when thenReturn.

Example:

Mastering Mockito in JUnit Testing: Simplifying Test Development

If in certain scenario we want to test exception block then we can use when-thenThrow.

Example:

Mastering Mockito in JUnit Testing: Simplifying Test Development

If we have any parameters to add in it we can do .

How to use doReturn when and doThrow when

For this we have to spy the classes. It does patial mocking means

Mock only specific methods while keeping real behaviour for the test.

It will actually call real implemenation in controlled manner.

We can do spy in this way

Mastering Mockito in JUnit Testing: Simplifying Test Development

Example:

Mastering Mockito in JUnit Testing: Simplifying Test Development

Thank You
Amarnath Mali
Helical IT Solutions

logo

Best Open Source Business Intelligence Software Helical Insight is Here

logo

A Business Intelligence Framework

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments