首页 > 学院 > 开发设计 > 正文

Spring 单元测试

2019-11-06 08:57:41
字体:
来源:转载
供稿:网友
@RunWith(SPRingJUnit4ClassRunner.class) @ContextConfiguration("/config/Spring-db1.xml") @Transactional public class AccountServiceTest1 { @Autowired private AccountService service; @Test public void testGetAcccountById() { Account acct = Account.getAccount(1, "user01", 18, "M"); service.insertIfNotExist(acct); Account acct2 = service.getAccountById(1); assertEquals(acct,acct2); } }

小结 如果您希望在 Spring 环境中进行单元测试,那么可以做如下配置: 继续使用 Junit4 测试框架,包括其 @Test 注释标签和相关的类和方法的定义,这些都不用变 您需要通过 @RunWith(SpringJUnit4ClassRunner.class) 来启动 Spring 对测试类的支持 您需要通过 @ContextConfiguration 注释标签来指定 Spring 配置文件或者配置类的位置 您需要通过 @Transactional 来启用自动的事务管理 您可以使用 @Autowired 自动织入 Spring 的 bean 用来测试

http://www.ibm.com/developerworks/cn/java/j-lo-springunitest/


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表