easyMockTest

什么是EasyMock? EasyMock是一套提供了通过简单的接口和方法对给定的对象生成Mock对象的类库,通过record,replay,verify 3个生命周期来帮助完成测试过程,通过EasyMock我们可以方便构造Mock对象从而进行单元测试的开发 为什么要使用EasyMock? 当我们编写单元测试的过程中,我们常常遇到应用中其他依赖模块尚未开发完成,或者该依赖的构建比较复杂的情况,例如Service层已经开发完成,DAO层却还在开发当中,但Service需要依赖DAO来进行测试,显然这种情况下Service是没有办法进行测试的,因为此时需要依赖DAO进行测试,又或者例如测试Servlet,Request和Session等都需要由服务器来生成,而Mock对象就是用来对一些未实现的关联对象或依赖对象的类进行测试的对象,EasyMock就是实现Mock对象的框架,例如Service依赖DAO,我们可以使用Mock对象来模拟DAO的实现或者模拟Request和Session,简单可以理解为Mock对象是模拟了我们给定接口实现的对象
//有返回值
private PrepareHotelS
erviceImpl prepareHotelService; private PrepareHotelDao prepareHotelDao; @Before public void setUp() { prepareHotelService = new PrepareHotelServiceImpl(); prepareHotelDao = EasyMock.createMock(PrepareHotelDao.class); prepareHotelService.setPrepareHotelDao(prepareHotelDao); } @Test public void testFindPrepareHotel() { ContextID contextID = new ContextID("IHG", "AROAN"); String source = "IHG.DMX"; PrepareHotel prepareHotel = new PrepareHotel(); prepareHotel.setContextId("AROAN"); prepareHotel.setContextName("IHG"); prepareHotel.setSource("IHG.DMX"); Set attributes = Sets.newHashSet(); attributes.add(new PrepareHotelAttribute("Name.EN-US", "Holiday Inn Andorra222", 1, prepareHotel)); attributes.add(new PrepareHotelAttribute("Location.Longitude", "1.5184222", 1, prepareHotel)); attributes.add(new PrepareHotelAttribute("Location.Latitude", "42.5054", 1, prepareHotel)); attributes.add(new PrepareHotelAttribute("Location.CountryCode", "AD", 1, prepareHotel)); attributes.add(new PrepareHotelAttribute("Location.CityName.EN-US", "Andorra", 1, prepareHotel)); attributes.add(new PrepareHotelAttribute("Contact.Phone", "+376874444", 1, prepareHotel)); attributes.add(new PrepareHotelAttribute("Contact.Fax", "+376874445", 1, prepareHotel)); attributes.add(new PrepareHotelAttribute("Contact.Email", "[email protected]", 1, prepareHotel)); attributes.add(new PrepareHotelAttribute("GuestRoom.Quantity.Total", "133", 1, prepareHotel)); attributes.add(new PrepareHotelAttribute("Hotel.Context.IHG.ID", "AROAN", 1, prepareHotel)); attributes.add(new PrepareHotelAttribute("Hotel.Context.TGS.ID", "1401546", 1, prepareHotel)); prepareHotel.setAttributes(attributes); PrepareHotel exceptPreparedHotel = new PrepareHotel(contextID.getName(), contextID.getId(), source, attributes); //prepareHotelDao EasyMock.expect(prepareHotelDao.findPrepareHotel(contextID, source)).andReturn(exceptPreparedHotel).times(1); EasyMock.replay(prepareHotelDao); PrepareHotel actualPrepareHotel = prepareHotelService.findPrepareHotel(contextID, source); EasyMock.verify(prepareHotelDao); Assert.assertEquals(exceptPreparedHotel, actualPrepareHotel); } 



//无返回值
@Test
public void testSaveOrUpdate() {
PrepareHotel prepareHotel = new PrepareHotel();
prepareHotel.setContextId("AROAN");
prepareHotel.setContextName("IHG");
prepareHotel.setSource("IHG.DMX");
Set<PrepareHotelAttribute> attributes = Sets.newHashSet();
attributes.add(new PrepareHotelAttribute("Name.EN-US", "Holiday Inn Andorra222", 1, prepareHotel));
attributes.add(new PrepareHotelAttribute("Location.Longitude", "1.5184222", 1, prepareHotel));
attributes.add(new PrepareHotelAttribute("Location.Latitude", "42.5054", 1, prepareHotel));
attributes.add(new PrepareHotelAttribute("Location.CountryCode", "AD", 1, prepareHotel));
attributes.add(new PrepareHotelAttribute("Location.CityName.EN-US", "Andorra", 1, prepareHotel));
attributes.add(new PrepareHotelAttribute("Contact.Phone", "+376874444", 1, prepareHotel));
attributes.add(new PrepareHotelAttribute("Contact.Fax", "+376874445", 1, prepareHotel));
attributes.add(new PrepareHotelAttribute("Contact.Email", "[email protected]", 1, prepareHotel));
attributes.add(new PrepareHotelAttribute("GuestRoom.Quantity.Total", "133", 1, prepareHotel));
attributes.add(new PrepareHotelAttribute("Hotel.Context.IHG.ID", "AROAN", 1, prepareHotel));
attributes.add(new PrepareHotelAttribute("Hotel.Context.TGS.ID", "1401546", 1, prepareHotel));
prepareHotel.setAttributes(attributes);

prepareHotelDao.saveOrUpdate(prepareHotel);
EasyMock.expectLastCall().times(1);
EasyMock.replay(prepareHotelDao);

prepareHotelService.saveOrUpdate(prepareHotel);
EasyMock.verify(prepareHotelDao);
}

你可能感兴趣的:(easymock)