Mockito-첫 번째 응용 프로그램
Mockito 프레임 워크의 세부 사항으로 들어가기 전에 애플리케이션이 작동하는지 살펴 보겠습니다. 이 예제에서는 일부 주식의 더미 가격을 가져 오기 위해 Stock Service의 모의를 만들고 Portfolio라는 Java 클래스를 단위 테스트했습니다.
프로세스는 아래에서 단계별로 설명합니다.
Step 1 − Create a JAVA class to represent the Stock
File: Stock.java
public class Stock {
private String stockId;
private String name;
private int quantity;
public Stock(String stockId, String name, int quantity){
this.stockId = stockId;
this.name = name;
this.quantity = quantity;
}
public String getStockId() {
return stockId;
}
public void setStockId(String stockId) {
this.stockId = stockId;
}
public int getQuantity() {
return quantity;
}
public String getTicker() {
return name;
}
}
Step 2 − Create an interface StockService to get the price of a stock
File: StockService.java
public interface StockService {
public double getPrice(Stock stock);
}
Step 3 − Create a class Portfolio to represent the portfolio of any client
File: Portfolio.java
import java.util.List;
public class Portfolio {
private StockService stockService;
private List<Stock> stocks;
public StockService getStockService() {
return stockService;
}
public void setStockService(StockService stockService) {
this.stockService = stockService;
}
public List<Stock> getStocks() {
return stocks;
}
public void setStocks(List<Stock> stocks) {
this.stocks = stocks;
}
public double getMarketValue(){
double marketValue = 0.0;
for(Stock stock:stocks){
marketValue += stockService.getPrice(stock) * stock.getQuantity();
}
return marketValue;
}
}
Step 4 − Test the Portfolio class
포트폴리오 클래스에 모의 주식 서비스를 삽입하여 테스트 해 보겠습니다. Mock은 Mockito에 의해 생성됩니다.
File: PortfolioTester.java
package com.tutorialspoint.mock;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.Mockito.*;
public class PortfolioTester {
Portfolio portfolio;
StockService stockService;
public static void main(String[] args){
PortfolioTester tester = new PortfolioTester();
tester.setUp();
System.out.println(tester.testMarketValue()?"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(StockService.class);
//set the stockService to the portfolio
portfolio.setStockService(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);
stocks.add(googleStock);
stocks.add(microsoftStock);
//add stocks to the portfolio
portfolio.setStocks(stocks);
//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);
double marketValue = portfolio.getMarketValue();
return marketValue == 100500.0;
}
}
Step 5 − Verify the result
다음을 사용하여 클래스 컴파일 javac 다음과 같이 컴파일러-
C:\Mockito_WORKSPACE>javac Stock.java StockService.java Portfolio.java PortfolioTester.java
이제 PortfolioTester를 실행하여 결과를 확인하십시오.
C:\Mockito_WORKSPACE>java PortfolioTester
출력 확인
pass