• spring junit4 test


    http://blog.csdn.net/feihong247/article/details/7828143
    http://my.oschina.net/anomynous/blog/168999

    1 引入spring-test
    1. <groupId>org.springframework</groupId>
    2. <artifactId>spring-test</artifactId>
    3. <version>${spring.version}</version>
    4. </dependency>
    2 代码,
    注意,sts中SpringJUnit4ClassRunner找不到,不会自动修复,
    1. package com.sgy.service;
    2. import static org.junit.Assert.fail;
    3. import java.util.Date;
    4. import javax.annotation.Resource;
    5. import org.junit.Test;
    6. import org.junit.runner.RunWith;
    7. import org.springframework.test.context.ContextConfiguration;
    8. import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
    9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    10. import com.sgy.entity.User;
    11. @RunWith(SpringJUnit4ClassRunner.class)
    12. @ContextConfiguration(locations = "classpath:spring.xml")
    13. public class UserServiceTest2 extends AbstractJUnit4SpringContextTests {
    14. @Resource(name="userService") UserService service;
    15. @Test
    16. public void testAdd() {
    17. User user=new User();
    18. user.setUsername("kkk3");
    19. user.setAddTime(new Date());
    20. try {
    21. service.save(user);
    22. System.out.println(user.getId());
    23. } catch (Exception e) {
    24. fail(e.getMessage());
    25. e.printStackTrace();
    26. }finally {
    27. service.delete(user);
    28. }
    29. }
    30. }
    3  service层已默认开启事务,如果测试dao层需要开启事务可以加上
    1. @Transactional
    2. @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)







  • 相关阅读:
    oracle 存储过程
    交错数组
    延迟加载
    js 闭包
    引用类型和值类型
    事务
    web api 之身份验证
    SQLServer中的服务器角色与数据库角色
    按照某一字段的相同值合并所对应的行的值
    VC工程中的字符集工程属性和字符编码(转)
  • 原文地址:https://www.cnblogs.com/stit/p/6368979.html
Copyright © 2020-2023  润新知