您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > junit
JUnit在SSH中的集成測(cè)試
作者:云中之歌 發(fā)布時(shí)間:[ 2017/5/12 11:14:34 ] 推薦標(biāo)簽:單元測(cè)試 測(cè)試用例

  測(cè)試Spring容器
  在Junit的測(cè)試類中,繼承AbstractJUnit4SpringContextTests可以進(jìn)行Spring容器測(cè)試, 例如下面測(cè)試用例,
1 @RunWith(SpringJUnit4ClassRunner.class)
2 @ContextConfiguration(locations = { "/applicationContext.xml",
3         "/daoContext.xml" })
4 public class TestSpring extends AbstractJUnit4SpringContextTests {
5
6     @Autowired
7     private ApplicationContext appContext;
8
9     @Autowired
10     private SessionFactory sessionFactory;
11
12     @Test
13     public void testSpringContext() {
14         EmpManager empMgr = (EmpManager) appContext.getBean("empManager");
15         List<AttendType> types = empMgr.getAllType();
16         for (AttendType type : types) {
17             System.out.println(type.getId() + "," + type.getAmerce());
18         }
19     }
20 }
  在AbstractJUnit4SpringContextTests中自帶一個(gè)applicationContext屬性變量,默認(rèn)使用applicationContext.xml進(jìn)行初始化,
  在子類中可以在類上重新指定新的配置文件,并再次定義ApplicationContext私有成員, 從而在測(cè)試方法中使用,如上。
  測(cè)試struts 的Action
  struts2提供了StrutsSpringTestCase這個(gè)類用來(lái)在junit中做測(cè)試, 只需要將測(cè)試類繼承此類即可,
  在測(cè)試方法中,將使用代理方法執(zhí)行Action
  在測(cè)試類中,通過(guò)重寫(xiě) getContextLocations方法,可以自定義配置文件,如下,
1 public class TestLoginAction extends StrutsSpringTestCase {
2
3     @Override
4     protected String[] getContextLocations() {
5         return new String[] { "classpath*:applicationContext.xml",
6                 "/daoContext.xml" };
7     }
8
9     public void testALogin() throws Exception {
10
11         request.setParameter("manager.name", "oracle");
12         request.setParameter("manager.pass", "oracle");
13         request.setParameter("vercode", "123456");
14
15         ActionProxy proxy = getActionProxy("/processLogin");
16         LoginAction loginAction = (LoginAction) proxy.getAction();
17
18         // setup httpsession before invoking Action to make it equals to the
19         // vercode code
20         Map<String, Object> httpSession = new HashMap<>();
21         httpSession.put("rand", "123456");
22         ServletActionContext.getContext().setSession(httpSession);
23         // inject beans for Action properties before executing Action.execute()
24         EmpManager mgr = (EmpManager) applicationContext.getBean("empManager");
25         loginAction.setEmpManager(mgr);
26
27         String result = proxy.execute();
28         assertTrue("result=" + result + "|"
29                 + loginAction.getActionMessages().toString(),
30                 result.equals("mgr"));
31         // assertTrue(ServletActionContext.getPageContext().getException().getMessage().toString(),result.equals("mgr"));
32
33     }
34
35     public void testPunchAction() throws Exception {
36         EmpManager empMgr = (EmpManager) applicationContext.getBean("empManager");
37         List<AttendType> types = empMgr.getAllType();
38         for (AttendType type : types) {
39             System.out.println(type.getId() + "," + type.getAmerce());
40         }
41     }
42 }
  需要注意的是上面的第25行的目的是為Action類中的屬性注入bean, 因?yàn)槲疫@里的Action bean并沒(méi)有放在Spring容器中托管, 在實(shí)際項(xiàng)目中,struts框架會(huì)自動(dòng)裝配Action的屬性, 但在這里需要手工裝配。
  另外繼承了StrutsSpringTestCase的測(cè)試類,不僅可以測(cè)試Action,同時(shí)也能測(cè)試Spring容器,例如上面的testPunchAction()方法。

軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd