qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          Spring集成TestNg測試

           1,在eclipse中安裝TestNg插件,這里省略。。
            2,編寫測試spring Dao層的代碼
          package test.com.smart.dao;
          import com.smart.dao.UserDao;
          import com.smart.domain.User;
          import com.smart.service.UserService;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.test.context.ContextConfiguration;
          import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
          import org.testng.annotations.Test;
          import java.util.Date;
          import static org.testng.Assert.assertEquals;
          import static org.testng.Assert.assertNotNull;
          import static org.testng.Assert.assertTrue;
          @ContextConfiguration(locations = {"classpath:resources/applicationContext.xml"})
          public class UserDaoTest extends AbstractTestNGSpringContextTests {
          @Autowired
          private UserDao userDao;
          @Test
          public void hasMatchUser() {
          int count  = userDao.getMatchCount("admin1", "123456");
          assertTrue(count>0);
          }
          @Test
          public void findUserByUserName() {
          User user = userDao.findUserByUserName("admin");
          assertNotNull(user);
          assertEquals(user.getUserName(), "admin");
          }
          }
            注意:@ContextConfiguration(locations = {"classpath:resources/applicationContext.xml"})這個注解是的localtion屬性使用的是src類路徑下面的

           resources/applicationContext.xml spring配置文件
            2,由于TestNg測試持久層和測試服務層十分類似,這里省略了,這里給出測試層的代碼
            a,項目編寫封裝了客戶端請求的類
          package com.smart.web;
          public class LoginCommand {
          private String userName;
          private String password;
          public String getPassword() {
          return password;
          }
          public void setPassword(String password) {
          this.password = password;
          }
          public String getUserName() {
          return userName;
          }
          public void setUserName(String userName) {
          this.userName = userName;
          }
          }
            b.下面編寫控制類的代碼
          package com.smart.web;
          import java.util.Date;
          import javax.servlet.http.HttpServletRequest;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.stereotype.Controller;
          import org.springframework.web.bind.annotation.RequestMapping;
          import org.springframework.web.servlet.ModelAndView;
          import com.smart.domain.User;
          import com.smart.service.UserService;
          @Controller
          @RequestMapping(value = "/admin")
          public class LoginController {
          @Autowired
          private UserService userService;
          @RequestMapping(value = "/login.html")
          public String loginPage() {
          return "login";
          }
          @RequestMapping(value = "/loginCheck.html")
          public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand) {
          boolean isValidUser =
          userService.hasMatchUser(loginCommand.getUserName(),
          loginCommand.getPassword());
          if (!isValidUser) {
          return new ModelAndView("login", "error", "用戶名或密碼錯誤。");
          } else {
          User user = userService.findUserByUserName(loginCommand
          .getUserName());
          user.setLastIp(request.getLocalAddr());
          user.setLastVisit(new Date());
          userService.loginSuccess(user);
          request.getSession().setAttribute("user", user);
          return new ModelAndView("main");
          }
          }
          }
          c,編寫好控制類的代碼,我們就可以測試這個控制類了,下面的代碼是使用TestNg測試controller十分正確
          package test.com.smart.web;
          import com.smart.domain.User;
          import com.smart.web.LoginController;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.mock.web.MockHttpServletRequest;
          import org.springframework.mock.web.MockHttpServletResponse;
          import org.springframework.test.context.ContextConfiguration;
          import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
          import org.springframework.test.util.ReflectionTestUtils;
          import org.springframework.web.client.RestTemplate;
          import org.springframework.web.servlet.HandlerMapping;
          import org.springframework.web.servlet.ModelAndView;
          import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
          import org.testng.annotations.BeforeMethod;
          import org.testng.annotations.Test;
          import static org.testng.Assert.assertEquals;
          import static org.testng.Assert.assertNotNull;
          import static org.testng.Assert.assertNull;
          @ContextConfiguration(locations = {"classpath:applicationContext.xml","file:d:/actionSpring/chapter/chapter1/src/main/webapp/WEB-INF/viewspace-servlet.xml"})
          public class LoginControllerTest extends AbstractTestNGSpringContextTests {
          @Autowired
          private AnnotationMethodHandlerAdapter handlerAdapter;
          @Autowired
          private LoginController controller;
          //聲明Request與Response模擬對象
          private MockHttpServletRequest request;
          private MockHttpServletResponse response;
          //執行測試前先初始模擬對象
          @BeforeMethod
          public void before() {
          request = new MockHttpServletRequest();
          request.setCharacterEncoding("UTF-8");
          response = new MockHttpServletResponse();
          request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true); //Spring3.1 存在的BUG
          }
          // 測試LoginController#loginCheck()方法
          @Test
          public void loginCheck() throws Exception {
          //測試登陸成功的情況
          request.setRequestURI("/admin/loginCheck.html");
          request.addParameter("userName", "admin"); // 設置請求URL及參數
          request.addParameter("password", "123456");
          //向控制發起請求 ” /loginCheck.html”
          ModelAndView mav = handlerAdapter.handle(request, response, controller);
          User user = (User) request.getSession().getAttribute("user");
          assertNotNull(mav);
          assertEquals(mav.getViewName(), "main");
          assertNotNull(user);
          request.getSession().removeAttribute("user");
          //測試登陸失敗的情況
          request.setRequestURI("/admin/loginCheck.html");
          request.addParameter("userName", "test");
          request.addParameter("password", "123456");
          mav = handlerAdapter.handle(request, response, controller);
          user = (User) request.getSession().getAttribute("user");
          assertNotNull(mav);
          assertEquals(mav.getViewName(), "login");
          assertNull(user);
          }
          }
            注意:
          @ContextConfiguration(locations = {"classpath:applicationContext.xml","file:d:/actionSpring/chapter/chapter1/src/main/webapp/WEB-INF/viewspace-servlet.xml"})  這個注解可以整合多個spring配置文件中"file:d:/actionSpring/chapter/chapter1/src/main/webapp/WEB-INF/viewspace-servlet.xml"表示的是文件系統的形式給出配置文件

          posted on 2014-01-07 10:48 順其自然EVO 閱讀(7116) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2014年1月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 莱阳市| 阿克| 天津市| 包头市| 宕昌县| 韶关市| 鄂伦春自治旗| 哈密市| 调兵山市| 井研县| 宕昌县| 华阴市| 措勤县| 北碚区| 临桂县| 梧州市| 凭祥市| 天台县| 锦州市| 民乐县| 大新县| 六安市| 原平市| 达尔| 象州县| 客服| 武穴市| 手游| 湖南省| 博客| 青浦区| 津市市| 青神县| 中西区| 界首市| 南靖县| 宁海县| 威海市| 高州市| 永胜县| 丹巴县|