学习testNg
来源:互联网 发布:linux 双网卡 同网段 编辑:程序博客网 时间:2024/06/07 11:26
使用springmvc 做开发时,需要对controller做测试,而有时候界面的设计比后台功能性的开发速度还要慢,又想测试controller 的正确性时,就可以用上这种方法。
下面是一个 junit case
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/applicationContext.xml","file:src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml","file:src/main/webapp/WEB-INF/datasource/datasource.xml"}) //使用注解加载spring的相关配置
public class TestEmailController {
private MockHttpServletRequest request; //模拟request
private MockHttpServletResponse response; //模拟response
@Autowired
private EmailController ec; //这里要注入controller 就是你调试要执行的controller
@Before //做初始化的工作
public void setUp(){
request = new MockHttpServletRequest();
request.setCharacterEncoding("UTF-8");
response = new MockHttpServletResponse();
}
@Test
public void emailDetail() { //用test标签可以分开测试不同的 controller
try {
assertEquals("data_indox_details",ec.emailDetail(41, request, response)) ;
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void emailContent(){
try{
assertEquals("data_indox_details_tail",ec.emailContent(3, request, response)) ;
}catch (Exception e) {
e.printStackTrace();
}
}
}
另外,在@Test标签可以做异常测试,比如
@Test(enabled=true,expectedExceptions = NullPointerException.class) //这里预期抛出空指针异常
在@Test注解中可以做超时测试。如果运行时间超过指定的毫秒数,就测试失败,这对于测试网络链接类十分重要
@Test(timeOut=10)
- TestNG 学习
- 学习testNg
- TestNG学习
- testNG的学习
- testNG 学习笔记1
- TestNG学习记录
- 学习:TestNg@DataProvider
- testNG学习点--零散
- TestNG学习(一)
- Selenium学习17--testng
- testNG单元测试学习
- 学习testNG安装
- testng学习日记-10/19
- testng学习日记-10/21
- testng学习日记-10/26
- 学习Testng遇到的问题
- TestNG学习记录--------基本注解
- testNG入门学习(一)
- Mybatis类型, Java类型和PostgreSQL数据库类型的对应关系(草稿)
- protobuf的Required,Optional,Repeated限定修饰符
- xenserver常见问题汇总
- Android NDK开发Crash错误定位
- 大型网站架构系列:电商网站架构案例
- 学习testNg
- Citrix ICA协议简介及与RDP协议对比—-- 理论篇
- Aircrack-ng使用说明
- bootstrap - 表格(条纹、边框、压缩)
- ace tree使用小记
- cmake设置msvc的运行库(runtime library)塈指定openjpeg使用静态库
- 进程地址空间
- 成为一个Java的架构师要学习哪些知识?
- 百度地图(一)