使用junit4来测试SSH

环境:基于SSH框架,外加3层结构(dao层、service层、action层) spring

1.针对action使用new一个对象

      IndexAction action =   new   IndexAction(); app


2.而后在@Before(若是测试方法之间没有数据交叉的部分的话,能够考虑使用BeforeClass来加载【只加载一次】)里面默认加载Spring的配置文件applicationContext.xml,由于hibernate是由spring来管理的,因此只须要加载spring的配置文件就好了:

      BeanFactory beanFactory   =   new   ClassPathXmlApplicationContext( "file:E:\\eclipseWorkspace\\project\WebRoot\\WEB-INF\\applicationContext.xml"   );  框架

//由于默认的applicationContext.xml的位置我没法肯定(貌似它是找的WebContent下的WEB-INF/classes的路径,可是个人applicationContext.xml放在WEB-INF下面,我用“../applicationContext.xml”却不行,牛人请指教),因此使用的是本地绝对路径,在加载绝对路径的时候,必须在前面加上(file:) eclipse


3.而后再有用到注入对象的地方使用beanFactory来getBean(),同时要把get到的bean赋值到对象的属性上面,供给调用:

      action. commonservice   = (CommonService) beanFactory   .getBean( "commonServices" ); 测试


4.最后断言结果是否与预测一致 spa

      Assert.assertEquals( "success" , result);
相关文章
相关标签/搜索