使用JUnit4对SSH2框架Service/Dao层进行单元测试
来源:互联网 发布:mysql 两个字段排序 编辑:程序博客网 时间:2024/04/28 11:16
- JUnit是一个非常好用的测试框架,但在对SSH架构的Java代码中需要注入由Spring管理的Bean,下面就简单介绍一下使用JUnit4对SSHSSH2框架Service/Dao层进行单元测试的方法。
- 在建立JUnit Test Case 测试类时,勾选setUpBeforeClass,我们需要在setUpBeforeClass()类中加载Spring配置文件。其它步骤和一般的Java测试过程一样(添加测试类的名称,选择需要测试的类和要测试的方法)。
- 建立好测试类后在setUpBeforeClass() 类中添加ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{"src/applicationContext.xml"});来加载配置文件,注意配置文件的路径(根据自己的配置文件位置选择)。然后使用context.getBean()获取对象。
- 示例:
public class testService{public static EnterpriseinfoServiceImpl service;@BeforeClasspublic static void setUpBeforeClass() throws Exception{System.out.println("加载配置文件……");ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{"src/applicationContext.xml"});System.out.println("加载配置文件成功");service = (EnterpriseinfoServiceImpl) context.getBean("enterpriseinfoService"); //enterpriseinfoService为applicationContext.xml配置文件中Service类对象id值}@Testpublic void testSave(){try{Enterpriseinfo info = new Enterpriseinfo();info.setEnglishabbreviation("Myenglishname");info.setEnglishfullname("myenglishfullname");info.setEnterpriseabbreviation("enterpriseabbreviation");info.setEnterprisefullname("enterprisefullname");info.setStockcode(12434);info.setId(3);service.save(info);} catch (Exception e){e.printStackTrace();}}}
注意:该测试方法测试后数据并不会自动回滚
3 0
- 使用JUnit4对SSH2框架Service/Dao层进行单元测试
- 用Spring提供的JUnit框架扩展对DAO或Service层进行单元测试
- 用Spring提供的JUnit框架扩展对DAO或Service层进行单元测试
- 用Spring提供的JUnit框架扩展对DAO或Service层进行单元测试
- dao层service层分别使用dbunit以及easymock进行单元测试
- 使用junit4+NekoHTML对页面进行单元测试
- junit4 测试dao层 和service层
- 使用JUnit4进行单元测试
- JUnit4单元测试框架使用
- Junit结合Spring对Dao层进行单元测试
- Junit结合Spring对Dao层进行单元测试
- Spring对Controller、Service、Dao进行Junit单元测试总结
- Spring对Controller、Service、Dao进行Junit单元测试总结
- junit4 对springmvc 进行单元测试
- Junit4对SSH Dao层进行单元测试,报错:WARN No appenders could be found for logger (org.springframework.context.su
- Eclipse使用jUnit4对MapReduce程序进行单元测试
- JUnit4基础 使用JUnit4进行单元测试
- 使用JUnit4进行单元测试(一)
- 图形编程——Paint和Canvas的基本使用
- set 的使用方法
- 马云太恐怖了
- 马云太恐怖了
- jsp防止跨域提交数据
- 使用JUnit4对SSH2框架Service/Dao层进行单元测试
- 女人与坟墓
- 黑马程序员_高新技术代理
- 常用的程序调试工具使用
- Microsoft Jet数据库引擎MDB文件解析远程栈溢出漏洞
- 判断应用是否显示
- 怎么把驱动编译进内核?
- 分析Windows和Linux动态库
- 使用Eclipse构建Maven的SpringMVC项目