function price = lookupPrice(ticker,date)
% This method assumes you have installed and configured the
% Bloomberg software.
conn = blp;
data = history(conn,ticker,'LAST_PRICE',date-1,date);
price = data(end);
close(conn)
end
end
endIn this example, assume that the broker component has not been developed yet. Once it is
implemented, it will have a buy method that accepts a ticker symbol and a specified
number of shares to buy, and returns a status code. The mock for the broker component
uses an implicit interface, and does not derive from a superclass.Component Under TestIn a file trader.m in your current working folder, create a simple day trading algorithm.
The trader function accepts as inputs a data service object that looks up the price of the
stock, a broker object that defines how the stock is bought, a ticker symbol, and a number
of shares to purchase. If the price from yesterday is less than the price two days ago,
instruct the broker to buy the specified number of shares.function trader(dataService,broker,ticker,numShares)
yesterday = datetime('yesterday');
priceYesterday = dataService.lookupPrice(ticker,yesterday);
price2DaysAgo = dataService.lookupPrice(ticker,yesterday-days(1));if priceYesterday < price2DaysAgo
broker.buy(ticker,numShares);
end
endMock Objects and Behavior ObjectsThe mock object is an implementation of the abstract methods and properties of the
interface specified by a superclass. You can also construct a mock without a superclass, in
which case the mock has an implicit interface. The mock object carries out the actions for
the component under test, such as calling a method or accessing a property.33 Unit Testing