Mockito - First Application - Tutorialspoint
Mockito - First Application - Tutorialspoint
Before going into the details of the Mockito Framework, let's see an application in action. In this example, we've created a mock of Stock
Service to get the dummy price of some stocks and unit tested a java class named Portfolio.
File: Stock.java
return quantity;
}
import java.util.List;
return stocks;
}
for(Stock stock:stocks){
marketValue += stockService.getPrice(stock) * stock.getQuantity();
}
return marketValue;
}
}
package com.tutorialspoint.mock;
import java.util.ArrayList;
import java.util.List;
Portfolio portfolio;
StockService stockService;
https://www.tutorialspoint.com/mockito/mockito_first_application.htm 3/5
9/22/2020 Mockito - First Application - Tutorialspoint
stocks.add(googleStock);
stocks.add(microsoftStock);
//mock the behavior of stock service to return the value of various stocks
when(stockService.getPrice(googleStock)).thenReturn(50.00);
when(stockService.getPrice(microsoftStock)).thenReturn(1000.00);
https://www.tutorialspoint.com/mockito/mockito_first_application.htm 4/5
9/22/2020 Mockito - First Application - Tutorialspoint
C:\Mockito_WORKSPACE>java PortfolioTester
pass
https://www.tutorialspoint.com/mockito/mockito_first_application.htm 5/5