JUnit Test WebService

来源:互联网 发布:js隐藏标签 编辑:程序博客网 时间:2024/06/08 03:38

对于编写Unit Test,不需要多少工作经验就可以进行Code,但是对于调用WebService的测试方法是有讲究的,通常情况下在TestDriven模式下我们编写的UnitTest不应该受WebService的限制,下面我们提供另外一种测试方法,这种方法采用EazyMock的技术。

1.在CommonObjectConfig.java的类中我们可以看到实例化了很多的bean,这样也不再需要配置文件进行加载了:

   @Bean
public DeliveryStrategy deliveryStrategy() {
return new DeliveryStrategy();
}

对于调用的是接口Interface则写法如下:

 @Bean
public MongoTemplate mongoTemplate() {
return EasyMock.createMock(MongoTemplate.class);
}


2.在BaseTest.java类中进行初始化各个接口用到的注入属性:

  @Before
public void init() throws Exception {
// setup relationship
castCrewModelLoader.setCastCrewCatalogService(castCrewCatalogService);
castCrewModelLoader.setServiceMetadataService(serviceMetadataService);
playlistModelLoader.setPlaylistCatalogService(playlistCatalogService);
playlistModelLoader.setServiceMetadataService(serviceMetadataService);
ratingMapper.setRatingCatalogService(ratingCatalogService);
programModelLoader.setFaaProgramService(programService);
programModelLoader.setBlueprintService(blueprintService);
seriesModelLoader.setFaaSeriesService(seriesService);
titleModelLoader.setFaaTitleService(titleService);
synopsisModelLoader.setFaaSynopsisService(synopsisService);
offeringModelLoader.setOfoOfferingService(ofoOfferingService);
seasonModelLoader.setFaaSeasonService(seasonService);
titleModelLoader.setBlueprintService(blueprintService);
synopsisModelLoader.setBlueprintService(blueprintService);
//load public data
EasyMock.reset(ratingCatalogService);

EasyMock.replay(ratingCatalogService);



}


3.在测试类DeliveryStrategyForEpisodeTest.java中拦截要访问的WebService并以预先准备好的数据返回:

EasyMock.reset(configRepository,endpointService,seriesService,programService, titleService,synopsisService);
 
//TODO ORCAEVENT PARAM hardcorde 2016-01-18
OrcaEvent orcaEvent = getOrcaEvent();


EasyMock.expect(configRepository.findByServiceAndOrcaEventAndSubUsageExactMatch(service,
orcaEvent.getEntityName(), orcaEvent.getSubUsage(), orcaEvent.getEntityGroup()))
.andReturn(loadConfigs(mercury_templateconfig_location,"hbo_episode_config.json")).anyTimes();

EasyMock.expect(endpointService.loadEndpointByName(EasyMock.anyString())).andAnswer(new IAnswer<Endpoint>() {
public Endpoint answer() throws Exception {
String arg1 = (String) getCurrentArguments()[0];
return loadEndpointByName(mercury_testDateFileLocation,"endpoint.json").get(arg1);
}
}).anyTimes();

EasyMock.expect(
programService.getCanonicalbyUrnId(EasyMock.eq("urn:esp:hbo:program:412347b9da014acdfb3298d0b453ffe1"),
EasyMock.anyObject(QueryOption.class)))
.andReturn(loadSeariesByName(mercury_faa_location,"hbo_program_data.json")).anyTimes();

EasyMock.expect(
seriesService.getSeriesByUrnId(EasyMock.eq("urn:esp:hbo:series:10df4f0b68ec381e389da27249cf6cd3"),
EasyMock.isA(QueryOption.class)))
.andReturn(loadSeariesByName(mercury_faa_location,"hbo_series_data.json")).once();

EasyMock.expect(
titleService.queryTitleByAssetTypeAndId(EasyMock.eq("hbous"),
EasyMock.eq("PRODUCT"), EasyMock.eq(namespaceId),
EasyMock.eq(longPattern)))
.andReturn(loadMapDataByFileName(mercury_faa_location,"hbo_longtitle_data.json")).anyTimes();

EasyMock.expect(
titleService.queryTitleByAssetTypeAndId(EasyMock.eq("hbous"),
EasyMock.eq("PRODUCT"), EasyMock.eq(namespaceId),
EasyMock.eq(shortPattern)))
.andReturn(loadMapDataByFileName(mercury_faa_location,"hbo_shorttitle_data.json")).anyTimes();



EasyMock.expect(
synopsisService.querySynopsisByAssetTypeAndId(EasyMock.eq("hbous"),
EasyMock.eq(assetType), EasyMock.eq(namespaceId),
EasyMock.eq(longPatternSyn)))
.andReturn(loadMapDataByFileName(mercury_faa_location,"hbo_longsynopsis_data.json")).anyTimes();


EasyMock.expect(
synopsisService.querySynopsisByAssetTypeAndId(EasyMock.eq("hbous"),
EasyMock.eq(assetType), EasyMock.eq(namespaceId),
EasyMock.eq(shortPatternSyn)))
.andReturn(loadMapDataByFileName(mercury_faa_location,"hbo_shortsynopsis_data.json")).anyTimes();


EasyMock.replay(configRepository,endpointService,seriesService,programService, titleService,synopsisService);



完成上述的Coding后就可以进行UnitTest,这时再Test就不需要依赖WebService了。


0 1
原创粉丝点击