Wednesday, September 14, 2016

Quick Notes About Mockito

Unit Test Case:

JUnit Test Case:

Usually mokito is used for unit test cases. JUnit is rarely used.

autowiring - saturday
sunday: spring


Popular Mocking Unit Test:
1. Mockito
2. Easy Mock
3. Power Mock

only mock the code which you cannot setup and ....
we dont mock an implementation


we should know what we have to mock in a method


go to mockito.org to see some really good documentation



Optimized the code
next step is: mock the object but was it really called?


@Mock

@RunWith --> if we want to do all @Mock annotations, then we need to do the @RunWith annotation.



Familierize with mockito, try easy mock as well as try also power mock


times, atLeast, atMost can be used to verify how many times the method is called.
verify(dao, times(2)).createProduct(any(ProductEntity.classs) )




First we have to create a happy path scenario, then do a test coverage....


spy is a partial mock, it will only mock the things that you will tell to mock.
mock is a total mock.

mockito is much better than easy mock. no one will insist you to do a particular mock in job.


----------------------
java ee example, where we have reservation controller or ui component calling into the reservation BO
which pose the business logic and the BO calls the DAO which does the DB operations using jdbc, hibernate etc.

if we are testing the reservation controller, we mock out the reservationBo
and all the methods on the reservationBo which reservation controller methods are using
and test all the public methods on the reservation controller in isolation...

similarly, if we are testing reservationBO, we do the same for reservationDAO.

No comments:

Post a Comment