原來對測試不夠重視,出現問題的時候,靠着開着服務器在哪調試,確實搞得很麻煩。。遇到問題想用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();
}
}
有空繼續更新!!