1 建立一個test的目錄,在此目錄下放置所有的JunitTestCase類和TestCase的配置檔案
2 將專案中的Spring配置檔案(預設名稱為applicationContext.xml)複製到test目錄下,並重新命名為JunitTestConf.xml。
3 根據Junit測試的需要修改JunitTestConf.xml檔案中的內容,如資料庫連線等。
4 新建一個名為SpringConfForTest.java的類,在此類中配置Spring啟動所需的配置檔案,並啟動Spring。此類的內容如下:
package test;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.soma.global.WebContextHolder;
public class SpringConfForTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//Spring啟動所需要的配置引數檔案,其中test/JunitTestConf.xml檔案中儲存了資料庫連線等引數,可根據具體情況做修改
String[] paths = new String[] {"test/JunitTestConf.xml", "com/soma/conf/applicationContext-dao-hr.xml","com/soma/conf/applicationContext-dao.xml","com/soma/conf/applicationContext-dao-bug.xml","com/soma/conf/applicationContext-dao-change.xml","com/soma/conf/applicationContext-dao-common.xml","com/soma/conf/applicationContext-service-hr.xml" };
//啟動Spring,得到Spring環境上下文
ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
//在此類啟動時,將Spring環境上下文儲存到單根類WebContextHolder中,以提供給其它的測試類使用
WebContextHolder.getInstence().setApplicationContext(ctx);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
@Test
public void test(){
//必須要寫一個test空方法,否則SpringConfForTest類不會啟動
5 新建TestSuite類,類名為AllTests,類的內容如下所示:
import junit.framework.Test;
import junit.framework.TestSuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import test.com.soma.domain.busilogic.hr.HrBusiLogicTest;
import test.com.soma.domain.service.hr.checkOverTimeDateTest;
@RunWith(Suite.class)
@Suite.SuiteClasses({
SpringConfForTest.class,
HrBusiLogicTest.class,
checkOverTimeDateTest.class
})
/**
* 批次執行Junit測試類,把類名寫入到上面的Suite.SuiteClasses({})中,用逗號分隔
*/
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for test");
//$JUnit-BEGIN$
//$JUnit-END$
return suite;
注意:將SpringConfForTest.class放在第一個執行,以啟動Spring配置環境,把自己的TestCase類放到後面,用逗號分開。在測試時,只要執行這個TestSuite類就可以了。
6 寫自己的TestCase類,以CheckOverTimeDateTest.java為例子,檔案內容如下:
public class CheckOverTimeDateTest {
private static HrTbovertimeManager hrTbovertimeManager;
private static ExcuteSqlDAO excuteSqlDAO;
//從Spring上下文中得到hrTbovertimeManager介面類的例項
hrTbovertimeManager=(HrTbovertimeManager)BeanUtil.getBean("hrTbovertimeManager");
excuteSqlDAO = (ExcuteSqlDAO) BeanUtil.getBean("excuteSqlDAO");
public void testGetProjectList()throws Exception {
List<OvertimeDetailValue> overtimeDetailValueList = new ArrayList<OvertimeDetailValue>();
int index = 9;
for(int i = 1 ;i <= index;i++){
OvertimeDetailValue overtimeDetailValue = new OvertimeDetailValue();
overtimeDetailValue.setOtApplyDate("2009-05-0"+i);
overtimeDetailValueList.add(overtimeDetailValue);
String resultStr = hrTbovertimeManager.checkOverTimeDate(overtimeDetailValueList);
assertEquals("false", resultStr);
public void testSaveExcelDutyInformation() throws Exception{
excuteSqlDAO.excuteSql("delete from hr_tbdutyinformation where dutydate>="2009-02-26" and dutydate<="2009-03-25"");
// System.out.println("----------"+System.getProperty("user.dir")+"/src/test/duty200903.xls");
String fileName = System.getProperty("user.dir")
+ "/src/test/duty200903.xls";
assertNull(hrTbdutyInformationManager.saveExcelDutyInformation(fileName));
說明:BeanUtil.getBean("")相當於WebContextHolder.getInstence().getApplicationContext().getBean(""),只是對此方法做了封裝。
7 在Eclipse中,啟動AllTests,選擇“Run As JunitTest”,即可先啟動Spring環境,再依次執行你自己所寫的JunitTestCase,是不是很簡單哪?趕快動手試試吧。
1 建立一個test的目錄,在此目錄下放置所有的JunitTestCase類和TestCase的配置檔案
2 將專案中的Spring配置檔案(預設名稱為applicationContext.xml)複製到test目錄下,並重新命名為JunitTestConf.xml。
3 根據Junit測試的需要修改JunitTestConf.xml檔案中的內容,如資料庫連線等。
4 新建一個名為SpringConfForTest.java的類,在此類中配置Spring啟動所需的配置檔案,並啟動Spring。此類的內容如下:
package test;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.soma.global.WebContextHolder;
public class SpringConfForTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//Spring啟動所需要的配置引數檔案,其中test/JunitTestConf.xml檔案中儲存了資料庫連線等引數,可根據具體情況做修改
String[] paths = new String[] {"test/JunitTestConf.xml", "com/soma/conf/applicationContext-dao-hr.xml","com/soma/conf/applicationContext-dao.xml","com/soma/conf/applicationContext-dao-bug.xml","com/soma/conf/applicationContext-dao-change.xml","com/soma/conf/applicationContext-dao-common.xml","com/soma/conf/applicationContext-service-hr.xml" };
//啟動Spring,得到Spring環境上下文
ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
//在此類啟動時,將Spring環境上下文儲存到單根類WebContextHolder中,以提供給其它的測試類使用
WebContextHolder.getInstence().setApplicationContext(ctx);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test(){
//必須要寫一個test空方法,否則SpringConfForTest類不會啟動
}
}
5 新建TestSuite類,類名為AllTests,類的內容如下所示:
package test;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import test.com.soma.domain.busilogic.hr.HrBusiLogicTest;
import test.com.soma.domain.service.hr.checkOverTimeDateTest;
@RunWith(Suite.class)
@Suite.SuiteClasses({
SpringConfForTest.class,
HrBusiLogicTest.class,
checkOverTimeDateTest.class
})
/**
* 批次執行Junit測試類,把類名寫入到上面的Suite.SuiteClasses({})中,用逗號分隔
*/
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for test");
//$JUnit-BEGIN$
//$JUnit-END$
return suite;
}
}
注意:將SpringConfForTest.class放在第一個執行,以啟動Spring配置環境,把自己的TestCase類放到後面,用逗號分開。在測試時,只要執行這個TestSuite類就可以了。
6 寫自己的TestCase類,以CheckOverTimeDateTest.java為例子,檔案內容如下:
public class CheckOverTimeDateTest {
private static HrTbovertimeManager hrTbovertimeManager;
private static ExcuteSqlDAO excuteSqlDAO;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//從Spring上下文中得到hrTbovertimeManager介面類的例項
hrTbovertimeManager=(HrTbovertimeManager)BeanUtil.getBean("hrTbovertimeManager");
excuteSqlDAO = (ExcuteSqlDAO) BeanUtil.getBean("excuteSqlDAO");
}
@Test
public void testGetProjectList()throws Exception {
List<OvertimeDetailValue> overtimeDetailValueList = new ArrayList<OvertimeDetailValue>();
int index = 9;
for(int i = 1 ;i <= index;i++){
OvertimeDetailValue overtimeDetailValue = new OvertimeDetailValue();
overtimeDetailValue.setOtApplyDate("2009-05-0"+i);
overtimeDetailValueList.add(overtimeDetailValue);
}
String resultStr = hrTbovertimeManager.checkOverTimeDate(overtimeDetailValueList);
assertEquals("false", resultStr);
}
/**
*/
@Test
public void testSaveExcelDutyInformation() throws Exception{
excuteSqlDAO.excuteSql("delete from hr_tbdutyinformation where dutydate>="2009-02-26" and dutydate<="2009-03-25"");
// System.out.println("----------"+System.getProperty("user.dir")+"/src/test/duty200903.xls");
String fileName = System.getProperty("user.dir")
+ "/src/test/duty200903.xls";
assertNull(hrTbdutyInformationManager.saveExcelDutyInformation(fileName));
}
}
說明:BeanUtil.getBean("")相當於WebContextHolder.getInstence().getApplicationContext().getBean(""),只是對此方法做了封裝。
7 在Eclipse中,啟動AllTests,選擇“Run As JunitTest”,即可先啟動Spring環境,再依次執行你自己所寫的JunitTestCase,是不是很簡單哪?趕快動手試試吧。