@Test public void testBuy() { long userId = 12345; UserDao daoMock = mock(UserDao.class); when(daoMock.queryById(anyLong())).thenReturn(new User(userId, "张三", 19)); String commodityId = "S01A10009823"; CommodityDao commodityDao = mock(CommodityDao.class); when(commodityDao.queryById(anyString())).thenReturn(new Commodity(commodityId, "xxx手机", 1)); _userService.setUserDao(daoMock); _userService.setCommodityDao(commodityDao); assertTrue(_userService.buy(userId, commodityId)); }
public boolean buy(long userId, String commodityId) { if (! isAdult(userId)) { return false; } Commodity commodity = _commodityDao.queryById(commodityId); if (null == commodity) { return false; } return true; }
UserService { public boolean buy(long userId, String commodityId) { if (! isAdult(userId)) { return false; } Commodity commodity = _commodityDao.queryById(commodityId); if (null == commodity) { return false; } return true; } }
UserService { public boolean buy(long userId, String commodityId) { if (! isAdult(userId)) { return false; } Commodity commodity = _commodityDao.queryById(commodityId); if (null == commodity) { return false; } return true; }  }
UserService { public boolean buy(long userId, String commodityId) { if (! isAdult(userId)) { return false; } Commodity commodity = _commodityDao.queryById(commodityId); if (null == commodity) { return false; } return true; }  boolean isAdult(long userId); boolean buy(long userId, String commodityId); UserDao getUserDao(); void setUserDao(UserDao userDao); CommodityDao getCommodityDao(); void setCommodityDao(CommodityDao commodityDao); }
UserService { public boolean buy(long userId, String commodityId) { if (! isAdult(userId)) { return false; } Commodity commodity = _commodityDao.queryById(commodityId); if (null == commodity) { return false; } return true; }  boolean isAdult(long userId); boolean buy(long userId, String commodityId); UserDao getUserDao(); void setUserDao(UserDao userDao); CommodityDao getCommodityDao(); void setCommodityDao(CommodityDao commodityDao);  }