您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源單元測(cè)試工具 > TestNG
Spring集成TestNg測(cè)試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2014/1/6 10:18:40 ] 推薦標(biāo)簽:TestNg Spring

  c,編寫好控制類的代碼,我們可以測(cè)試這個(gè)控制類了,下面的代碼是使用TestNg測(cè)試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模擬對(duì)象
private MockHttpServletRequest request;
private MockHttpServletResponse response;
//執(zhí)行測(cè)試前先初始模擬對(duì)象
@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
}
// 測(cè)試LoginController#loginCheck()方法
@Test
public void loginCheck() throws Exception {
//測(cè)試登陸成功的情況
request.setRequestURI("/admin/loginCheck.html");
request.addParameter("userName", "admin"); // 設(shè)置請(qǐng)求URL及參數(shù)
request.addParameter("password", "123456");
//向控制發(fā)起請(qǐng)求 ” /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");
//測(cè)試登陸失敗的情況
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)的形式給出配置文件

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