1、加入相应依赖包

junit4-4.7.jar 以及spring相关jar包

2、在测试代码的源码包中如 src/test/java 新建一个抽象类如下

 import org.junit.runner.RunWith;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.test.context.transaction.TransactionConfiguration;
 import org.springframework.transaction.annotation.Transactional;
 /**
  * 所有测试类的基类
  */
 @RunWith(SpringJUnit4ClassRunner.class)
 /**
  * 多个配置文件以逗号隔开,如果配置文件在WEB-INF目录下可使用file进行配置具体如下
  * @ContextConfiguration(locations = {"classpath:applicationContext.xml","file:WebRoot/WEB-INF/config/applicationContext-*.xml"})
  */
 @ContextConfiguration(locations = {"classpath:applicationContext.xml"})
 /**
  * 用到事物管理时的配置,defaultRollback=true 表示执行完毕回滚
  */
 @Transactional
 @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
 public abstract class AbstractTestBase {
 }

3、测试

 import org.junit.Test;
 import org.springframework.beans.factory.annotation.Autowired;

 import com.blog.model.User;

 public class UserServiceTest extends AbstractTestBase{

     @Autowired
     UserService userService;

     @Test
     public void testAdd(){
         User user = new User();
         user.setUsername("cyhe");
         user.setPassword("1234");
         boolean bool = userService.registerUser(user);
         if(bool == true){
             System.out.println("用户添加成功");
         } else {
             System.out.println("用户添加失败");
         }
     }
 }

可以看到自动去加载相关的配置文件,最终显示添加成功

Spring整合Junit4的更多相关文章

  1. Spring 整合 Junit4 进行单元测试

    1. pom.xml 引入JAR依赖: <dependency> <groupId>junit</groupId> <artifactId>junit& ...

  2. Spring整合JUnit4进行AOP单元测试的时候,报:"C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3\lib\idea_rt.jar=64

    错误代码 "C:\Program Files\Java\jdk1.8.0_191\bin\java.exe" -ea -Didea.test.cyclic.buffer.size= ...

  3. Spring整合JUnit4测试

    @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:spring/ap ...

  4. Spring整合Junit4进行单元测试

    一. 添加依赖包(maven) <dependency> <groupId>junit</groupId> <artifactId>junit</ ...

  5. Spring整合JUnit4测试使用注解引入多个配置文件

    转自:https://kanpiaoxue.iteye.com/blog/2151903 我们使用spring写junit单测的时候,有的时候我们的spring配置文件只有一个.我们在类的注释上面会这 ...

  6. Spring整合JUnit4测试时,使用注解引入多个配置文件

    转自:https://blog.csdn.net/pwh309315228/article/details/62226372 一般情况下: @ContextConfiguration(Location ...

  7. Spring的AOP开发入门,Spring整合Junit单元测试(基于ASpectJ的XML方式)

    参考自 https://www.cnblogs.com/ltfxy/p/9882430.html 创建web项目,引入jar包 除了基本的6个Spring开发的jar包外,还要引入aop开发相关的四个 ...

  8. Spring整合Redis&JSON序列化&Spring/Web项目部署相关

    几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...

  9. Spring Test+JUnit4整合使用测试ZZJ_淘淘商城项目:day01(RESTful Web Service)

    针对整合的Dao层与Service层,在做spring与通用Mapper和分页插件相关测试时比较麻烦.如果只用JUnit测试,需要每次Test方法里初始化一下applicationContext,效率 ...

随机推荐

  1. C#使用读写锁三行代码简单解决多线程并发写入文件时线程同步的问题

    (补充:初始化FileStream时使用包含文件共享属性(System.IO.FileShare)的构造函数比使用自定义线程锁更为安全和高效,更多内容可点击参阅) 在开发程序的过程中,难免少不了写入错 ...

  2. thinkphp中assign()和display()区别和用法

  3. 【Java每日一题】20161206

    package Dec2016; public class Ques1206 { public static void main(String[] args){ doSex(null); } publ ...

  4. MySQL建表规范与常见问题

    一. 表设计 库名.表名.字段名必须使用小写字母,“_”分割. 库名.表名.字段名必须不超过12个字符. 库名.表名.字段名见名知意,建议使用名词而不是动词. 建议使用InnoDB存储引擎. 存储精确 ...

  5. js中实现中文按字母拼音排序

    js中实现中文按字母拼音排序 var Pinyin = (function (){ var Pinyin = function (ops){ this.initialize(ops); }, opti ...

  6. Scalaz(26)- Lens: 函数式不可变对象数据操作方式

    scala中的case class是一种特殊的对象:由编译器(compiler)自动生成字段的getter和setter.如下面的例子: case class City(name:String, pr ...

  7. 容器--Map和AbstractMap

    一.前言 前面我们介绍了Collection接口的定义及一系列实现,并重点介绍了List体系下的一些实现,对于Collection来说,其实还有Set系列同样很重要,但由于Set是依赖于Map实现的, ...

  8. selector 的用法,在选择和不选择情况下的颜色

    在res/drawable文件夹新增一个文件,此文件设置了图片的触发状态,你可以设置 state_pressed,state_checked,state_pressed,state_selected, ...

  9. buffer和cache有什么本质区别

    在free命令展示机器的内存消耗情况,会像这样展示

  10. php高级

    php面试题之一--PHP核心技术(高级部分) 一.PHP核心技术 1.写出一个能创建多级目录的PHP函数(新浪网技术部) <?php /** * 创建多级目录 * @param $path s ...