2 将项目中的Spring配置文件(默认名称为applicationContextxml)复制到test目录下,并重新命名为JunitTestConfxml。
3 根据Junit测试的需要修改JunitTestConfxml文件中的内容,如数据库连接等。
4 新建一个名为SpringConfForTestjava的类,在此类中配置Spring启动所需的配置文件,并启动Spring。此类的内容如下:
package test;
import orgjunitAfterClass;
import orgjunitBeforeClass;
import orgjunitTest;
import orgspringframeworkcontextApplicationContext;
import orgspringframeworkcontextsupportClassPathXmlApplicationContext;
import comsomaglobalWebContextHolder;
public class SpringConfForTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//Spring启动所需要的配置参数文件,其中test/JunitTestConfxml文件中保存了数据库连接等参数,可根据具体情况做修改
String[] paths = new String[] {"test/JunitTestConfxml", "com/soma/conf/applicationContext-dao-hrxml","com/soma/conf/applicationContext-daoxml","com/soma/conf/applicationContext-dao-bugxml","com/soma/conf/applicationContext-dao-changexml","com/soma/conf/applicationContext-dao-commonxml","com/soma/conf/applicationContext-service-hrxml" };
//启动Spring,得到Spring环境上下文
ApplicationContext ctx = new ClassPathXmlApplicationContext(paths);
//在此类启动时,将Spring环境上下文保存到单根类WebContextHolder中,以提供给其它的测试类使用
WebContextHoldergetInstence()setApplicationContext(ctx);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test(){
//必须要写一个test空方法,否则SpringConfForTest类不会启动
}
}
5 新建TestSuite类,类名为AllTests,类的内容如下所示:
package test;
import junitframeworkTest;
import junitframeworkTestSuite;
import orgjunitrunnerRunWith;
import orgjunitrunnersSuite;
import testcomsomadomainbusilogichrHrBusiLogicTest;
import testcomsomadomainservicehrcheckOverTimeDateTest;
@RunWith(Suiteclass)
@SuiteSuiteClasses({
SpringConfForTestclass,
HrBusiLogicTestclass,
checkOverTimeDateTestclass
})
/
批量执行Junit测试类,把类名写入到上面的SuiteSuiteClasses({})中,用逗号分隔
/
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for test");
//$JUnit-BEGIN$
//$JUnit-END$
return suite;
}
}
注意:将SpringConfForTestclass放在第一个执行,以启动Spring配置环境,把自己的TestCase类放到后面,用逗号分开。在测试时,只要执行这个TestSuite类就可以了。
6 写自己的TestCase类,以CheckOverTimeDateTestjava为例子,文件内容如下:
public class CheckOverTimeDateTest {
private static HrTbovertimeManager hrTbovertimeManager;
private static ExcuteSqlDAO excuteSqlDAO;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//从Spring上下文中得到hrTbovertimeManager接口类的实例
hrTbovertimeManager=(HrTbovertimeManager)BeanUtilgetBean("hrTbovertimeManager");
excuteSqlDAO = (ExcuteSqlDAO) BeanUtilgetBean("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();
overtimeDetailValuesetOtApplyDate("2009-05-0"+i);
overtimeDetailValueListadd(overtimeDetailValue);
}
String resultStr = hrTbovertimeManagercheckOverTimeDate(overtimeDetailValueList);
assertEquals("false", resultStr);
}
/
导入2009-03月份出勤记录excel文件,返回null表示导入成功,需要先删除3月份的数据
/
@Test
public void testSaveExcelDutyInformation() throws Exception{
// 在导入3月份出勤记录前先删除3月份的记录,执行delete from hr_tbdutyinformation;
excuteSqlDAOexcuteSql("delete from hr_tbdutyinformation where dutydate>='2009-02-26' and dutydate<='2009-03-25'");
// Systemoutprintln("----------"+SystemgetProperty("userdir")+"/src/test/duty200903xls");
String fileName = SystemgetProperty("userdir")
+ "/src/test/duty200903xls";
assertNull(hrTbdutyInformationManagersaveExcelDutyInformation(fileName));
}
}
说明:BeanUtilgetBean("")相当于WebContextHoldergetInstence()getApplicationContext()getBean(""),只是对此方法做了封装。
7 在Eclipse中,启动AllTests,选择“Run As JunitTest”,即可先启动Spring环境,再依次运行你自己所写的JunitTestCase,是不是很简单哪?赶快动手试试吧。public class HelloWorld {
public String say() {
return("Hello World!");
}
}
public class First extends TestCase {
public First(String name) {
super(name);
}
public void testSay() {
HelloWorld hi = new HelloWorld();
assertEquals("Hello World!", hisay());
}
public static void main(String[] args) {
junittextuiTestRunnerrun(Firstclass);
}
}@Test
@ContextConfiguration(locations = { "/applicationContextxml" })
@TransactionConfiguration(defaultRollback = false)
public class CatTest extends SpringTxTestCase{
@Autowired
private CatServiceImpl catServiceImpl;
@Test
public void testCat() throws Exception{
Cat cat = catServiceImplfindById(1);
Systemoutprintln(catgetName());
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)