精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-03-10
skydream 写道 1)导致多次Spring容器初始化问题
这个有个简单的方法解决,我原来遇到过类似的问题,测试action或者service时,因为我的代码是需要先启动连接池的。 当时解决的方法非常简单,给所有测试案例都来一个基类,然后在setup()方法中初始化连接池,为了防止每个TestCase都重新执行一遍连接池初始化,给这个基类设置一个static的标志位 代码类似: public class AbstarctTestCase extends TestCase { private static boolean inited = false; public void setup() { super.setup(); if (!inited) { init(); inited = true; } } } 或者把上下文在基类里做成单态的,呵呵,差不多的做法 |
|
返回顶楼 | |
发表时间:2008-03-11
还是看这里吧
http://www.infoq.com/articles/testing-in-spring |
|
返回顶楼 | |
发表时间:2008-03-14
有没有具体实例代码,支不支持增量测试,下面是我用junit的测试,也只要加载一次
//TestSuite public class AllTests { private static ApplicationContext context; public static Test suite() { //初始化spring String[] configLocations = {"WebRoot/WEB-INF/spring-action.xml","WebRoot/WEB-INF/spring-service.xml","WebRoot/WEB-INF/spring-dao.xml"}; context = new FileSystemXmlApplicationContext(configLocations); CommonServiceImplTest.setCommonService(context); TestSuite suite = new TestSuite("Test for com.hrtc.testcase"); // $JUnit-BEGIN$ suite.addTestSuite(CommonServiceImplTest.class); // $JUnit-END$ return suite; } } //TestCase public class CommonServiceImplTest extends TestCase { private static ICommonService commonService; /** * 获得spring context * @param context */ public static void setCommonService(ApplicationContext context){ commonService = (ICommonService) context.getBean("commonService"); } @Test public void testGetAdminByLoginName() { try { Admin admin = commonService.getAdminByLoginName("hrtc"); System.out.println("loginname:"+admin.getLoginName()); } catch (CommonException e) { e.printStackTrace(); } } @Test public void testCheckUserExist(){ try { System.out.println("check admin hrtc is exist or not"); boolean exist = commonService.checkUserExist("hrtc",true); System.out.println(exist); System.out.println("check user hrtc is exist or not"); exist = commonService.checkUserExist("hrtc",false); System.out.println(exist); } catch (CommonException e) { e.printStackTrace(); this.fail("验证用户是否存在失败"); } } } |
|
返回顶楼 | |
发表时间:2008-03-15
hrtc 写道 有没有具体实例代码,支不支持增量测试,下面是我用junit的测试,也只要加载一次
//TestSuite public class AllTests { private static ApplicationContext context; public static Test suite() { //初始化spring String[] configLocations = {"WebRoot/WEB-INF/spring-action.xml","WebRoot/WEB-INF/spring-service.xml","WebRoot/WEB-INF/spring-dao.xml"}; context = new FileSystemXmlApplicationContext(configLocations); CommonServiceImplTest.setCommonService(context); TestSuite suite = new TestSuite("Test for com.hrtc.testcase"); // $JUnit-BEGIN$ suite.addTestSuite(CommonServiceImplTest.class); // $JUnit-END$ return suite; } } //TestCase public class CommonServiceImplTest extends TestCase { private static ICommonService commonService; /** * 获得spring context * @param context */ public static void setCommonService(ApplicationContext context){ commonService = (ICommonService) context.getBean("commonService"); } @Test public void testGetAdminByLoginName() { try { Admin admin = commonService.getAdminByLoginName("hrtc"); System.out.println("loginname:"+admin.getLoginName()); } catch (CommonException e) { e.printStackTrace(); } } @Test public void testCheckUserExist(){ try { System.out.println("check admin hrtc is exist or not"); boolean exist = commonService.checkUserExist("hrtc",true); System.out.println(exist); System.out.println("check user hrtc is exist or not"); exist = commonService.checkUserExist("hrtc",false); System.out.println(exist); } catch (CommonException e) { e.printStackTrace(); this.fail("验证用户是否存在失败"); } } } ajoo 写道 Spring那个不叫“单元”测试。
|
|
返回顶楼 | |
发表时间:2008-03-15
引用 3)数据库现场容易遭受破坏
测试方法对数据库的更改操作会持久化到数据库中。虽然是针对开发数据库进行操作,但如果数据操作的影响是持久的,可能会影响到后面的测试行为。举个例子,用户在测试方法中插入一条ID为1的User记录,第一次运行不会有问题,第二次运行时,就会因为主键冲突而导致测试用例失败。所以应该既能够完成功能逻辑检查,又能够在测试完成后恢复现场,不会留下“后遗症”; -->使用Spring测试套件,Spring会在你验证后,自动回滚对数据库的操作,保证数据库的现场不被破坏,因此重复测试不会发生问题! 可以使用内存数据库 |
|
返回顶楼 | |