小弟刚刚在学单元测试。看书和视频感觉还行,但是在实际项目中遇到问题
PriceIndustryTypeImpl.java
public void addPriceIndustryType(Price price, long parentIndustryTypeId,
long industryTypeId) throws HibernateException { this.getHibernateTemplate().save(price);
PriceIndustryType pit = new PriceIndustryType();
pit.setIndustryType((IndustryType) this.getSession().load(
IndustryType.class, industryTypeId));
pit.setPrice(price); this.getHibernateTemplate().save(pit); }
这个怎么测试啊,我都蒙了。不会活用啊!既有参数,还存入数据库,有高人指点让我看mock,我看了,但还是不会。求达人指点。
PriceIndustryTypeImpl.java
public void addPriceIndustryType(Price price, long parentIndustryTypeId,
long industryTypeId) throws HibernateException { this.getHibernateTemplate().save(price);
PriceIndustryType pit = new PriceIndustryType();
pit.setIndustryType((IndustryType) this.getSession().load(
IndustryType.class, industryTypeId));
pit.setPrice(price); this.getHibernateTemplate().save(pit); }
这个怎么测试啊,我都蒙了。不会活用啊!既有参数,还存入数据库,有高人指点让我看mock,我看了,但还是不会。求达人指点。
import java.util.List;import org.hibernate.LockMode;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Transactional;import com.model.AccountIn;
import com.model.Employee;
import com.model.ReceivePayPlan;
import com.model.SellOrder;
import com.service.AccountInService;@Transactional
public class AccountInServiceImpl extends HibernateDaoSupport implements
AccountInService{ public void add(AccountIn t) throws Exception {
// TODO Auto-generated method stub
SellOrder sellOrder=t.getSellOrder();
if(sellOrder!=null&&sellOrder.getId()!=null){
getHibernateTemplate().load(SellOrder.class,sellOrder.getId());
t.setSellOrder(sellOrder);
}
getHibernateTemplate().save(t);
} @SuppressWarnings("unchecked")
@Transactional( readOnly = true)
public List<AccountIn> findAll() {
// TODO Auto-generated method stub
return getHibernateTemplate().loadAll(AccountIn.class);
}
@Transactional(readOnly = true)
public void lock(List<AccountIn> list) {
// TODO Auto-generated method stub
for (AccountIn a : list)
getHibernateTemplate().lock(a, LockMode.NONE);
}
public AccountIn findByID(Integer id) {
// TODO Auto-generated method stub
return (AccountIn) getHibernateTemplate().get(AccountIn.class, id);
} public List<AccountIn> findByDateTime(Date beginDate, Date endDate) {
// TODO Auto-generated method stub
// return getHibernateTemplate().findByNamedQuery("findByACCIDateTime",new Object[]{beginDate,endDate});
return null;
} public List<AccountIn> findByName(String name) {
// TODO Auto-generated method stub
return getHibernateTemplate().findByNamedQuery("findByACCIName",name);
} public List<AccountIn> findByType(String type) {
// TODO Auto-generated method stub
return getHibernateTemplate().findByNamedQuery("findByACCIType",type);
} public List<AccountIn> findByEmployeeName(Employee employee) {
// TODO Auto-generated method stub
return getHibernateTemplate().findByNamedQuery("findByACCIEmployeeName",employee.getName());
} public void delete(AccountIn t) throws Exception {
// TODO Auto-generated method stub
getHibernateTemplate().delete(t);
} public void add(AccountIn accountIn, Integer receive) {
// 保存这条记录
logger.debug("1");
getHibernateTemplate().save(accountIn);
// 更新回款计划状态
logger.debug("2");
ReceivePayPlan receivePayPlan = (ReceivePayPlan) getHibernateTemplate().get(ReceivePayPlan.class, receive);
receivePayPlan.setState("1");
getHibernateTemplate().update(receivePayPlan);
// 更新对应的销售单状态
logger.debug("3");
SellOrder sellOrder = receivePayPlan.getSellOrder();
sellOrder.setPaymentMoney(sellOrder.getPaymentMoney()+receivePayPlan.getMoney());
sellOrder.setLeaveMoney(sellOrder.getTotalMoney()-sellOrder.getPaymentMoney());
if (sellOrder.getTotalMoney()==sellOrder.getPaymentMoney()) {
sellOrder.setState("2");
}
getHibernateTemplate().update(sellOrder);
logger.debug("4");
}
}
package com.test;import java.util.HashMap;
import java.util.List;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.test.AbstractTransactionalSpringContextTests;
import com.model.AccountIn;
import com.service.AccountInService;public class TestAccountInServiceImpl extends AbstractTransactionalSpringContextTests { @Override
protected String[] getConfigLocations() { return new String[] { "classpath:app*.xml" };
} public void testGet() {
AccountInService accountInService=getService();
AccountIn accountIn=accountInService.findByID(1);
assertNotNull(accountIn);
} public void testSave() {
AccountInService accountInService= getService();
AccountIn accountIn=new AccountIn();
accountIn.setId(111);
accountIn.setVersion(0);
accountIn.setName("wewe");
try {
accountInService.add(accountIn);
} catch (Exception e) {
fail(e.toString());
}
flush();
assertTrue(true);
}
public void testLoadAll() {
AccountInService accountInService= getService();
List<AccountIn> aList=accountInService.findAll();
assertNotNull(aList); }
private void flush() {
Session ssn = SessionFactoryUtils.getSession(
(SessionFactory) applicationContext.getBean("sessionFactory"),
false);
ssn.flush();
} private AccountInService getService(){
return (AccountInService) applicationContext.getBean("AccountInService");
}
}
不是特别明白????????????????
然后写测试类,每个测试类都继承那个测试的基类.;
对于参数对象,如果能够通过new 关键字实例化的就直接new一个出来,如果像HttpServletRequest 这样通过环境实例化的对象就用mock
Mock mock = new Mock(javax.servlet.HttpServletRequest.class);
mock.expects(once()).method("getParameter").with(eq("name")).will(returnValue("xxxxxx"));
HttpServletRequest request = (HttpServletRequest)mock.proxy();