9/22/2020 Mockito - First Application - Tutorialspoint
Mockito - First Application
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.
The process is discussed below in a step-by-step manner.
Step 1 − Create a JAVA class to represent the Stock
File: [Link]
public class Stock {
private String stockId;
private String name;
private int quantity;
public Stock(String stockId, String name, int quantity){
[Link] = stockId;
[Link] = name;
[Link] = quantity;
}
public String getStockId() {
return stockId;
}
public void setStockId(String stockId) {
[Link] = stockId;
}
public int getQuantity() {
[Link] 1/5
9/22/2020 Mockito - First Application - Tutorialspoint
return quantity;
}
public String getTicker() {
return name;
}
}
Step 2 − Create an interface StockService to get the price of a stock
File: [Link]
public interface StockService {
public double getPrice(Stock stock);
}
Step 3 − Create a class Portfolio to represent the portfolio of any client
File: [Link]
import [Link];
public class Portfolio {
private StockService stockService;
private List<Stock> stocks;
public StockService getStockService() {
return stockService;
}
public void setStockService(StockService stockService) {
[Link] = stockService;
}
public List<Stock> getStocks() {
[Link] 2/5
9/22/2020 Mockito - First Application - Tutorialspoint
return stocks;
}
public void setStocks(List<Stock> stocks) {
[Link] = stocks;
}
public double getMarketValue(){
double marketValue = 0.0;
for(Stock stock:stocks){
marketValue += [Link](stock) * [Link]();
}
return marketValue;
}
}
Step 4 − Test the Portfolio class
Let's test the Portfolio class, by injecting in it a mock of stockservice. Mock will be created by Mockito.
File: [Link]
package [Link];
import [Link];
import [Link];
import static [Link].*;
public class PortfolioTester {
Portfolio portfolio;
StockService stockService;
[Link] 3/5
9/22/2020 Mockito - First Application - Tutorialspoint
public static void main(String[] args){
PortfolioTester tester = new PortfolioTester();
[Link]();
[Link]([Link]()?"pass":"fail");
}
public void setUp(){
//Create a portfolio object which is to be tested
portfolio = new Portfolio();
//Create the mock object of stock service
stockService = mock([Link]);
//set the stockService to the portfolio
[Link](stockService);
}
public boolean testMarketValue(){
//Creates a list of stocks to be added to the portfolio
List<Stock> stocks = new ArrayList<Stock>();
Stock googleStock = new Stock("1","Google", 10);
Stock microsoftStock = new Stock("2","Microsoft",100);
[Link](googleStock);
[Link](microsoftStock);
//add stocks to the portfolio
[Link](stocks);
//mock the behavior of stock service to return the value of various stocks
when([Link](googleStock)).thenReturn(50.00);
when([Link](microsoftStock)).thenReturn(1000.00);
[Link] 4/5
9/22/2020 Mockito - First Application - Tutorialspoint
double marketValue = [Link]();
return marketValue == 100500.0;
}
}
Step 5 − Verify the result
Compile the classes using javac compiler as follows −
C:\Mockito_WORKSPACE>javac [Link] [Link] [Link] [Link]
Now run the PortfolioTester to see the result −
C:\Mockito_WORKSPACE>java PortfolioTester
Verify the Output
pass
[Link] 5/5