亚洲好骚综合-亚洲黄色录像-亚洲黄色网址-亚洲黄色网址大全-99久久99久久-99久久99久久精品国产

您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
JUnit在SSH中的集成測試
作者:云中之歌 發布時間:[ 2017/5/12 11:14:34 ] 推薦標簽:單元測試 測試用例

  測試Spring容器
  在Junit的測試類中,繼承AbstractJUnit4SpringContextTests可以進行Spring容器測試, 例如下面測試用例,
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中自帶一個applicationContext屬性變量,默認使用applicationContext.xml進行初始化,
  在子類中可以在類上重新指定新的配置文件,并再次定義ApplicationContext私有成員, 從而在測試方法中使用,如上。
  測試struts 的Action
  struts2提供了StrutsSpringTestCase這個類用來在junit中做測試, 只需要將測試類繼承此類即可,
  在測試方法中,將使用代理方法執行Action
  在測試類中,通過重寫 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, 因為我這里的Action bean并沒有放在Spring容器中托管, 在實際項目中,struts框架會自動裝配Action的屬性, 但在這里需要手工裝配。
  另外繼承了StrutsSpringTestCase的測試類,不僅可以測試Action,同時也能測試Spring容器,例如上面的testPunchAction()方法。

軟件測試工具 | 聯系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd
主站蜘蛛池模板: 浪浪视频成版人在线观看 | 天天碰夜夜 | 国产韩国精品一区二区三区久久 | 免费色在线 | 国产伦精品一区二区三区无广告 | 午夜剧场免费观看 | 久久青草18免费观看网站 | 日本欧美一级二级三级不卡 | 波多野吉衣在线多野结衣 | 中文字幕天天躁日日躁狠狠 | 免费人成网站线观看合集 | 韩国三级理伦片 | 污黄视频在线观看 | 麻豆导航| 韩国日本三级在线 | 青青青青久久精品国产h | 99国产精品久久久久久久成人热 | www.a级片| 久青草国产在视频在线观看 | 天天躁日日躁狠狠躁欧美日韩 | 久青草中文字幕精品视频 | 毛片毛片大全aaaa | 午夜影院在线播放 | 午夜视频一区 | 国产大片视频免费观看 | 国产人人干 | 婷婷伊人网 | 久久97精品久久久久久久不卡 | 精品一区二区视频在线观看 | 日本日b| 国产麻豆精品在线观看 | 色黄在线 | 亚洲天天做日日做天天看 | 极品美女丝袜被的网站 | 免费一级毛片正在播放 | 午夜一级片| 香蕉视频久久 | 免费的三级毛片 | 国产成人亚洲精品影院 | 中国高清videossex免费 | 国产精品人人爽人人做 |