生活随笔
收集整理的這篇文章主要介紹了
Spring集成TestNg测试
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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"})這個(gè)注解是的localtion屬性使用的是src類路徑下面的resources/applicationContext.xml spring配置文件
2,由于TestNg測試持久層和測試服務(wù)層十分類似,這里省略了,這里給出測試層的代碼 a,項(xiàng)目編寫封裝了客戶端請求的類 | 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", "用戶名或密碼錯(cuò)誤。"); } 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,編寫好控制類的代碼,我們就可以測試這個(gè)控制類了,下面的代碼是使用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; //執(zhí)行測試前先初始模擬對象 @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"); // 設(shè)置請求URL及參數(shù) request.addParameter("password", "123456"); //向控制發(fā)起請求 ” /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"}) ?這個(gè)注解可以整合多個(gè)spring配置文件中"file:d:/actionSpring/chapter/chapter1/src/main/webapp/WEB-INF/viewspace-servlet.xml"表示的是文件系統(tǒng)的形式給出配置文件
最新內(nèi)容請見作者的GitHub頁:http://qaseven.github.io/
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的Spring集成TestNg测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。