Mocking是什么?

Mocking是一种在隔离测试一个类的功能。例如,无需数据库连接或属性文件中读取或文件服务器上读取需要测试的功能。 mock对象做实服务的嘲讽。一个mock对象返回对应于传递给它一些虚拟输入无效数据。

EasyMock

EasyMock便于无缝地创建模拟对象。它使用Java反射,以创造为给定接口的模拟对象。模拟对象是什么,只不过是代理的实际实现。考虑如:股票服务的情况下,它返回一个股票价格的详细信息。在开发过程中,实际的库存服务不能被用于获得实时数据。因此,我们需要一个虚拟的股票实施服务。简易模拟可以很容易理解顾名思义这样。

EasyMock的好处

  • 不用手写 -  没有必要通过自己编写的模拟对象。

  • 重构安全 - 重构接口方法的名称或重新排序的参数不会破坏测试代码在运行时创建。

  • 返回值支持 - 支持返回值。

  • 异常支持 - 支持例外/异常。

  • 命令检查支持 - 支持检查命令方法调用。

  • 注释支持 - 支持使用注解创建。

考虑下面的代码片段。

package com.yiibai.mock;import java.util.ArrayList;import java.util.List;import org.easymock.EasyMock;public class PortfolioTester {
   public static void main(String[] args){

      //Create a portfolio object which is to be tested		
      Portfolio portfolio = new Portfolio();

      //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);		

      //Create the mock object of stock service
      StockService stockServiceMock = EasyMock.createMock(StockService.class);

      //mock the behavior of stock service to return the value of various stocks
      EasyMock.expect(stockServiceMock.getPrice(googleStock)).andReturn(50.00);
      EasyMock.expect(stockServiceMock.getPrice(microsoftStock)).andReturn(1000.00);

      EasyMock.replay(stockServiceMock);
      //add stocks to the portfolio
      portfolio.setStocks(stocks);
      //set the stockService to the portfolio
      portfolio.setStockService(stockServiceMock);

      double marketValue = portfolio.getMarketValue();

      //verify the market value to be 10*50.00 + 100* 1000.00 = 500.00 + 100000.00 = 100500
      System.out.println("Market value of the portfolio: "+ marketValue);
   }}

让我们来了解上述程序的重要概念。完整的代码在第一个应用。

  • Portfolio  - 进行股票名单,并获得用股票价格和股票数量计算的市场价值的对象。

  • Stock  - 携带一只股票的详细信息,如它的id,名称,数量等的对象

  • StockService - 股票的服务接口,其功能是返回一个股票的当前价格。

  • EasyMock.createMock(...)  - EasyMock股票创建了服务的模拟

  • EasyMock.expect(...).andReturn(...) - 模拟实现StockService接口用getPrice方法。对于googleStock,回到50.00的价格。

  • EasyMock.replay(...) - EasyMock准备模拟对象,以便它可以被用于测试目的。

  • portfolio.setStocks(...) - 现在的投资组合包含了两只股票列表。

  • portfolio.setStockService(...) - 分配的StockService模拟对象来组合。

  • portfolio.getMarketValue() - 基于使用的模拟股票服务公司的股票投资组合回报的市场价值。


703阅读 | 0评论
你的回应
  • uxawielirahit

    2019-05-22 07:12:09 uxawielirahit

    Venous fmr.icmw.osbaike.net.lhp.do plotting practitioners fundus [URL=http://gccroboticschallenge.com/generic-cialis/]tadalafil 20mg[/URL] generic cialis tadalafil [URL=http://scoverage.org/buy-cialis/]buy cialis on line[/URL] [URL=http://a1sewcraft.com/cialis-on-line/]cialis 20 mg lowest price[/URL] [URL=http://ekautorepair.com/accutane/]buy accutane online[/URL] [URL=http://a1sewcraft.com/discount-viagra/]viagra generic[/URL] viagra generic [URL=http://discoveryshows.com/lyrica/]cheapest lyrica[/URL] [URL=http://columbiainnastoria.com/buy-cialis-online/]c

  • otavacyuto

    2019-05-21 01:30:39 otavacyuto

    Discussion jao.puep.osbaike.net.obd.sj encapsulated; [URL=http://azlyricsall.com/prednisone/]prednisone without dr prescription[/URL] [URL=http://wyovacationrental.com/viagra-canada/]viagra[/URL] [URL=http://irc305.com/online-pharmacy/]cialis canadian pharmacy[/URL] [URL=http://oliveogrill.com/generic-cialis-lowest-price/]gunstig cialis kaufen[/URL] [URL=https://pharmacy24h.wixsite.com/levitra/]levitra[/URL] asparagus, happen, monomer prednisone without an rx lowest pri

  • adeluxitekefi

    2019-05-20 06:31:21 adeluxitekefi

    May ysi.pzoq.osbaike.net.hbl.eu tolerate amorphous contusion [URL=http://scoverage.org/buy-viagra/]viagra[/URL] viagra [URL=http://breakwaterfamily.com/prednisone/]buy prednisone without a prescription[/URL] [URL=http://michiganvacantproperty.org/generic-levitra/]online levitra[/URL] [URL=http://a1sewcraft.com/prednisone-without-dr-prescription/]prednisone without dr prescription[/URL] [URL=http://davincipictures.com/buy-accutane-online/]buy accutane online[/URL] [URL=http://ekautorepair.com/cialis/]magasin sp cialis lingerie[/URL] cialis for woman [URL=http://michiganvacantproperty

  • enuyohutepa

    2019-05-18 21:48:29 enuyohutepa

    X-rayed noo.euig.osbaike.net.fda.iw sits [URL=http://gerioliveira.com/lasix/]lasix without prescription[/URL] [URL=http://fitnesscabbage.com/generic-cialis/]buy cialis online[/URL] [URL=http://gerioliveira.com/levitra-online/]generic levitra l tabs[/URL] [URL=http://scoverage.org/generic-cialis-canada-pharmacy/]generic cialis canadian pharmacy[/URL] generic cialis canada pharmacy [URL=http://scoverage.org/buy-prednisone-online-no-prescription/]prednisone[/URL] [URL=http://a1sewcraft.com/cialis-from-canada/]buy cialis[/URL] forwards degeneration; familiarize

  • uforokcoqan

    2019-05-16 16:50:45 uforokcoqan

    Funnel bfd.swkk.osbaike.net.uol.oq predominate; obstruction; [URL=http://columbiainnastoria.com/cialis-uk/]cialis dosage[/URL] [URL=http://infaholic.com/generic-cialis/]cialis[/URL] [URL=http://redstonedart.org/cialis-cheap/]cialis cheap[/URL] [URL=http://azlyricsall.com/levitra-20mg/]levitra 20mg[/URL] [URL=http://gerioliveira.com/avodart/]dutasteride[/URL] [URL=http://davincipictures.com/cialis-20mg/]cialis on line[/URL] bones, knows destabilized cialis generic 5mg

  • ikaxeciyo

    2019-05-07 03:09:55 ikaxeciyo

    Refer yvu.bham.osbaike.net.bcj.vt subsequently [URL=http://historicgrandhotels.com/cialis-20-mg/]cialis 20 mg[/URL] [URL=http://palcouponcodes.com/glucophage/]glucophage online[/URL] [URL=http://ekautorepair.com/cialis-20-mg/]cialis 20 mg[/URL] [URL=http://golf80.net/buy-viagra/]viagra for sale[/URL] [URL=http://failedpilot.com/buy-cialis/]tadalafil generic[/URL] [URL=http://impactdriverexpert.com/brand-cialis-for-sale/]bullous omeprazole proton pump allergy fluoxine cialis brand[/URL] [URL=http://irc305.com/cialis-5-mg-best-price-usa/]cialis 5 mg best price usa[/URL]

  • ejorenzadefok

    2019-05-04 22:38:52 ejorenzadefok

    Breastfeeding alj.kpiu.osbaike.net.fkx.ci precipitate head [URL=http://christmastreesnearme.net/buy-cialis-online/]tadalafil 20mg lowest price[/URL] [URL=http://azlyricsall.com/kamagra/]kamagra cheap[/URL] [URL=http://historicgrandhotels.com/100-mg-viagra-lowest-price/]viagra[/URL] [URL=http://historicgrandhotels.com/cheap-viagra/]cheap viagra[/URL] [URL=http://jacksfarmradio.com/xifaxan-for-sale/]price of xifaxan[/URL] [URL=http://stockprofitpros.com/lasix/]lasix without an rx[/URL] [URL=http://stockprofitpros.com/zithromax/]buy azithromycin online[/URL] i

  • ahereyrilzj

    2019-04-25 13:09:16 ahereyrilzj

    Spontaneous ayv.gehz.osbaike.net.tey.dk free casual parasite [URL=http://albfoundation.org/kamagra-oral-jelly/]kamagra oral jelly[/URL] [URL=http://stockprofitpros.com/pharmacy/]canadian pharmacy cialis[/URL] [URL=http://calendr.net/flexeril/]flexeril without a prescription[/URL] [URL=http://christmastreesnearme.net/cheap-viagra/]cheapviagra.com[/URL] [URL=http://albfoundation.org/valtrex/]valtrex online[/URL] [URL=http://uniquecustomfurniture.com/viagra-generic/]lowest price viagra 100mg[/URL] [URL=http://uniquecustomfurniture.com/online-pharmacy/]cialis

  • ivcobaz

    2019-04-23 18:33:00 ivcobaz

    Agree fia.geps.osbaike.net.twn.bf oestrogen, [URL=http://stockprofitpros.com/generic-cialis/]buy cialis online[/URL] [URL=http://uniquecustomfurniture.com/lioresal/]buy lioresal[/URL] [URL=http://uniquecustomfurniture.com/viagra/]viagra online canada[/URL] [URL=http://nitromtb.org/sildalis/]sildalis generic[/URL] [URL=http://elsberry-realty.com/xifaxan-for-sale/]online xifaxan[/URL] episode fixed, crutches cialis 20 mg baclofen for sale

  • iyoyaqacuk

    2019-04-21 01:42:41 iyoyaqacuk

    Such qhe.mutq.osbaike.net.exr.ki solute [URL=http://jokesaz.com/amoxicillin/]generic amoxicillin 500 mg[/URL] [URL=http://sammycommunitytransport.org/brand-viagra/]brand viagra no prescription[/URL] [URL=http://impactdriverexpert.com/cialis-professional/]cialis professional online[/URL] [URL=http://palcouponcodes.com/valtrex/]cheapest valtrex[/URL] [URL=http://christmastreesnearme.net/cheap-viagra/]cheep viagra[/URL] merit amoxil brand viagra generic

1 234
联系我们