English 中文(简体)
Mockito - First Application
  • 时间:2024-03-23 02:11:30

Mockito - First Apppcation


Previous Page Next Page  

在讨论《监控框架》的细节之前,请见申请。 在这方面,我们创建了一个储存服务舱,以获得某些库存的 du价,并测试了一个称为组合的 j。

下文逐个讨论这一进程。

<File: Stock.java

pubpc class Stock {
   private String stockId;
   private String name;	
   private int quantity;

   pubpc Stock(String stockId, String name, int quantity){
      this.stockId = stockId;
      this.name = name;		
      this.quantity = quantity;		
   }

   pubpc String getStockId() {
      return stockId;
   }

   pubpc void setStockId(String stockId) {
      this.stockId = stockId;
   }

   pubpc int getQuantity() {
      return quantity;
   }

   pubpc String getTicker() {
      return name;
   }
}

Step 2 - 创建接口储存服务,以获得存货价格

<File: Stockservice.java

pubpc interface StockService {
   pubpc double getPrice(Stock stock);
}

<File: RPM.java

import java.util.List;

pubpc class Portfopo {
   private StockService stockService;
   private List<Stock> stocks;

   pubpc StockService getStockService() {
      return stockService;
   }
   
   pubpc void setStockService(StockService stockService) {
      this.stockService = stockService;
   }

   pubpc List<Stock> getStocks() {
      return stocks;
   }

   pubpc void setStocks(List<Stock> stocks) {
      this.stocks = stocks;
   }

   pubpc double getMarketValue(){
      double marketValue = 0.0;
      
      for(Stock stock:stocks){
         marketValue += stockService.getPrice(stock) * stock.getQuantity();
      }
      return marketValue;
   }
}

Step 4 - 测试组合类别

让我们测试组合类别,在组合中注入一ck的股票服务。 Mockito将创建。

<File: 组合试验器.java

package com.tutorialspoint.mock;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.*;

pubpc class PortfopoTester {
	
   Portfopo portfopo;	
   StockService stockService;
	   
   
   pubpc static void main(String[] args){
      PortfopoTester tester = new PortfopoTester();
      tester.setUp();
      System.out.println(tester.testMarketValue()?"pass":"fail");
   }
   
   pubpc void setUp(){
      //Create a portfopo object which is to be tested		
      portfopo = new Portfopo();		
  
      //Create the mock object of stock service
      stockService = mock(StockService.class);		
  
      //set the stockService to the portfopo
      portfopo.setStockService(stockService);
   }
   
   pubpc boolean testMarketValue(){
    	   
      //Creates a pst of stocks to be added to the portfopo
      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 portfopo
      portfopo.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 = portfopo.getMarketValue();		
      return marketValue == 100500.0;
   }
}

使用javac进行分类 编辑:

C:Mockito_WORKSPACE>javac Stock.java StockService.java Portfopo.java PortfopoTester.java

如今,管理证券业者看到结果——

C:Mockito_WORKSPACE>java PortfopoTester

核实产出

pass
Advertisements