1.

Can You Explain A Mockito Framework?

Answer»

In Mockito, you always check a PARTICULAR class. The dependency in that class is injected using mock object. So, for EXAMPLE, if you have service class, then the Dao class are injected as a mockDao. This enables us to check only the method of that given service class and whether they are PERFORMING as expected or not. 

For example, suppose, service class has an updateObject Method. That update method is depend on Dao1, Dao2. Here the objective is to test only the updateObject method and not the Dao1 and Dao2. So, we can create a mockDao and return a mock object. This object is not the one which is in the database but is custom made. It only tests whether the updates are happening as expected or not. The syntax to create a Mock object is as follows:

private STATIC LedgerBook mockLedgerBook;
In the setup(), you create the mock object
setup(){
mockLedgerBook = Mockito.mock(LedgeBookDao.class);
}
In the test method, you call this mockLedgerBook as follows:
LedgerBook ledgerBook = new LedgerBook();
ledgerBook.setName("default");
Mockito.when(mockLedgerBook.findBookById(Mockito.anyLong()).THENRETURN(ledgerBook);

In Mockito, you always check a particular class. The dependency in that class is injected using mock object. So, for example, if you have service class, then the Dao class are injected as a mockDao. This enables us to check only the method of that given service class and whether they are performing as expected or not. 

For example, suppose, service class has an updateObject Method. That update method is depend on Dao1, Dao2. Here the objective is to test only the updateObject method and not the Dao1 and Dao2. So, we can create a mockDao and return a mock object. This object is not the one which is in the database but is custom made. It only tests whether the updates are happening as expected or not. The syntax to create a Mock object is as follows:

private static LedgerBook mockLedgerBook;
In the setup(), you create the mock object
setup(){
mockLedgerBook = Mockito.mock(LedgeBookDao.class);
}
In the test method, you call this mockLedgerBook as follows:
LedgerBook ledgerBook = new LedgerBook();
ledgerBook.setName("default");
Mockito.when(mockLedgerBook.findBookById(Mockito.anyLong()).thenReturn(ledgerBook);



Discussion

No Comment Found