Energy of Love |
|
|||
日歷
統(tǒng)計
導(dǎo)航常用鏈接留言簿隨筆分類
隨筆檔案
搜索最新評論
閱讀排行榜評論排行榜 |
版權(quán)聲明:轉(zhuǎn)載時請以超鏈接形式標(biāo)明文章原始出處和作者信息及本聲明 參考:http://www.javaeye.com/topic/14631 背景: JUnit4的好處是:采用annotation來代替反射機制,不必寫死方法名. package testcase; import org.hibernate.FlushMode; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.orm.hibernate3.SessionFactoryUtils; import org.springframework.orm.hibernate3.SessionHolder; import org.springframework.transaction.support.TransactionSynchronizationManager;
/*** * An abstract base class for TestCases. * All test cases should extend this class. */ public class AbstractBaseTestCase {
private SessionFactory sessionFactory; private Session session; protected FileSystemXmlApplicationContext dsContext; private String []configStr = {"/WebRoot/WEB-INF/applicationContext.xml"};
@Before public void openSession() throws Exception { dsContext = new FileSystemXmlApplicationContext(configStr); sessionFactory = (SessionFactory) dsContext.getBean("sessionFactory"); session = SessionFactoryUtils.getSession(sessionFactory, true); session.setFlushMode(FlushMode.MANUAL); TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session)); }
@After public void closeSession() throws Exception { TransactionSynchronizationManager.unbindResource(sessionFactory); SessionFactoryUtils.releaseSession(session, sessionFactory); } } 接下來繼承上述基類,實現(xiàn)測試邏輯: (注意import static用于引用某個類的靜態(tài)方法) (@Test注解表明該方法是一個測試方法) package testcase; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import org.junit.Before; import org.junit.Test; public class testCase1 extends AbstractBaseTestCase { private YourManager manager; @Before public void prepare(){ manager = (YourManager)dsContext.getBean("YourManager"); } @Test public void test1(){ try { String result = manager.do_sth(); System.out.println(result); assertEquals(result, EXPECTED_RESULT); } catch (Exception e) { e.printStackTrace(); fail("Exception thrown."); } } }
|
![]() |
|
Copyright © 不高興 | Powered by: 博客園 模板提供:滬江博客 |