- Mockito - Discussion
- Mockito - Useful Resources
- Mockito - Quick Guide
- Mockito - Timeouts
- Behavior Driven Development
- Mockito - Resetting Mock
- Mockito - Spying
- Mockito - Callbacks
- Mockito - Ordered Verification
- Mockito - Create Mock
- Mockito - Exception Handling
- Mockito - Varying Calls
- Mockito - Expecting Calls
- Mockito - Verifying Behavior
- Mockito - Adding Behavior
- Mockito - JUnit Integration
- Mockito - First Application
- Mockito - Environment Setup
- Mockito - Overview
- Mockito - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Mockito - First Apppcation
在讨论《监控框架》的细节之前,请见申请。 在这方面,我们创建了一个储存服务舱,以获得某些库存的 du价,并测试了一个称为组合的 j。
下文逐个讨论这一进程。
<File: Stock.java Step 2 - 创建接口储存服务,以获得存货价格 <File: Stockservice.java <File: RPM.java Step 4 - 测试组合类别 让我们测试组合类别,在组合中注入一ck的股票服务。 Mockito将创建。 <File: 组合试验器.java 使用javac进行分类 编辑: 如今,管理证券业者看到结果—— 核实产出
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;
}
}
pubpc interface StockService {
pubpc double getPrice(Stock stock);
}
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;
}
}
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;
}
}
C:Mockito_WORKSPACE>javac Stock.java StockService.java Portfopo.java PortfopoTester.java
C:Mockito_WORKSPACE>java PortfopoTester
pass
Advertisements