原来对测试不够重视,出现问题的时候,靠着开着服务器在哪调试,确实搞得很麻烦。。遇到问题想用junit 进行单元测试的时候,却发现忘了怎么来做了,真是悲哀!现在总结下,以后就不会发生这种事了。。
1、Hibernate 的语句测试
SessionFactory sf=new Configuration().configure().buildSessionFactory();
Session session=sf.openSession();
Query q=session.createQuery("from Article a where a.banner.banner.id=1");
List l=q.list();
configure()方法默认会在classpath下面寻找hibernate.cfg.xml或者hibernate.properties文件。如果名字不同的话,则用Configuration cfg = new Configuration().configure("myexample.xml");
2、Spring 测试
public abstract class SpringTestCaseBase extends AbstractTransactionalDataSourceSpringContextTests {
protected SimpleDateFormat sdf;
public SpringTestCaseBase() {
// query the protected variables to implement denpendency injection automatically,
// so we don't need to write settor and gettor methods anymore.
this.setPopulateProtectedVariables(true);
sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(TimeZone.getDefault());
}
protected String[] getConfigLocations() {
return new String[] { "file:web/WEB-INF/applicationContext*.xml",
"file:web/WEB-INF/test-ApplicationContext*.xml"};
}
protected void flushSession(){
SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
sessionFactory.getCurrentSession().flush();
}
}
有空继续更新!!