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了。
- JUnit Test WebService
- junit test
- junit test
- junit-test
- junit @Test
- Junit 4 test suit
- android junit test问题
- Junit test EJB3
- JUnit test with Memcached
- android junit test问题
- junit test application代码
- android junit test
- Android JUnit Test
- Junit Test problem
- JUnit test suite 结构
- JUnit多线程Test
- JUnit Test NOSQL
- Android Junit Test Process
- uvaoj-1225:数数字
- spring(AOP)多个切面
- Leetcode之first missing positive
- XML VS SQLite
- POJ 1985 Cow Marathon(树的直径)
- JUnit Test WebService
- 可以输入的下拉框,可以筛选内容,可输入筛选下拉框(原创)
- Leetcode之Sort List
- HDU-1010-Tempter of the Bone( DFS + 深度优先搜索 )
- 在java中的基础点
- ubuntu 搭建(编译)生产力版的LNMP环境
- RHCE 学习笔记(20) ACL
- 下载并安装libevent开发库详细步骤
- 我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。