Spring对junit的整合

 package cn.mepu.service;

 import cn.mepu.config.SpringConfiguration;
 import cn.mepu.domain.Account;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

 import java.util.List;

 /**
  * @author shkstart
  * @create 2019-11-08 10:45
  * junit的整合:
  *  junit不会关注我们是否使用Spring框架,所以加了@Autowired注解也不能注入
  *  说明测试方法执行时没有ioc容器
  *      方法:换了junit的main方法在pom中导入spring-test
  *      使用junit提供的@Runwith注解把main方法替换为spring提供的传入字节码
  *      告知spring的运行器spring和ioc是基于注解还是xml,说明位置
  *          @ContextConfiguration
  *              locations=xml的位置加classpath关键字表示类路径下
  *              classes=指定注解位置
  *      当使用spring5.x版本时,junit的jar包必须是4.1.2及以上
  */
 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(classes = SpringConfiguration.class)
 public class AccountServiceTest {

     @Autowired
     private AccountService service;

     @Test
     public void testFindAll(){
     //3.执行方法
         List<Account> accounts = service.findAllAccount();
         for (Account account : accounts) {
             System.out.println("account = " + account);
         }

     }

     @Test
     public void testFindOne(){
         //3.执行方法
         Account account = service.findAccountById(1);
         System.out.println(account);
     }

     @Test
     public void testSave(){
         //3.执行方法
         service.saveAccount(new Account(1,"DDD",1234));
     }

     @Test
     public void testUpdate(){
         //3.执行方法
         service.updateAccount(new Account(1,"DDD",2345));
     }

     @Test
     public void testDelete(){

         //3.执行方法
         service.deleteAccount(4);
     }
 }

Spring对junit的整合的更多相关文章

  1. Maven聚合、Maven仓库jar包以及Spring+MyBatis+JUnit+Maven整合测试的搭建过程

    一.Maven将父项目创建到父项目的内部 在父项目的pom.xml上 点右键,选择maven-->new-->maven module  project 二.Maven聚合 在某个项目的p ...

  2. Spring与Junit测试整合

    一.引入spring测试包:text包 二.@RunWith:指定spring对junit提供的一个运行器 @ContextConfiguration:  locations指定spring配置文件位 ...

  3. Spring集成JUnit测试

    1.程序中有Junit环境2.导入一个jar包.spring与junit整合jar包 spring-test-3.2.0.RELEASE.jar3.测试代码 @RunWith(SpringJUnit4 ...

  4. 基于maven进行spring 和mybatis的整合(Myeclpise)

    学习日记:基于maven进行spring和mybatis的整合,进行分页查询 什么是maven:maven是一个项目管理工具,使用maven可以自动管理java项目的整个生命周期,包括编译.构建.测试 ...

  5. spring和redis的整合

    spring和redis的整合-超越昨天的自己系列(7) 超越昨天的自己系列(7) 扯淡:  最近一直在慢慢多学习各个组件,自己搭建出一些想法.是一个涉猎的过程,慢慢意识到知识是可以融汇贯通,举一反三 ...

  6. Spring+SpringMVC+MyBatis+easyUI整合基础篇(六)maven整合SSM

    写在前面的话   承接前文<Spring+SpringMVC+MyBatis+easyUI整合基础篇(五)讲一下maven>,本篇所讲述的是如何使用maven与原ssm项目整合,使得一个普 ...

  7. spring和hibernate的整合

    阅读目录 一.概述 二.整合步骤 1.大致步骤 2.具体分析 一.概述 Spring整合Hibernate有什么好处? 1.由IOC容器来管理Hibernate的SessionFactory 2.让H ...

  8. mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(附demo和搭建过程遇到的问题解决方法)

    文章介绍结构一览 一.使用maven创建web项目 1.新建maven项目 2.修改jre版本 3.修改Project Facts,生成WebContent文件夾 4.将WebContent下的两个文 ...

  9. Spring+Spring MVC+Mybatis 框架整合开发(半注解半配置文件)

    项目结构: (代码里面都有注释) 一.在pom文件中依赖jar包 因为我这里分了模块,所以有父子级的共两个pom文件 父级: <?xml version="1.0" enco ...

随机推荐

  1. VMware新建虚拟机之后的初始化工作

    一.开启网络功能(后面的ifcfg-ens33自身系统不同) vi /etc/sysconfig/network-scripts/ifcfg-ens33 ONBOOT=yes systemctl re ...

  2. docker使用entrypoint执行时报permission denied错误

    在Dockerfile中使用指令ENTRYPOINT来执行项目下entrypoint.shshell文件,如下: ENTRYPOINT ["./entrypoint.sh"] 时报 ...

  3. SpringMVC以及SSM整合

    本人才疏学浅,如有错误欢迎批评!转载请注明出处:https://www.cnblogs.com/lee-yangyaozi/p/11226145.html SpringMVC概述 Spring Web ...

  4. 配置基于python的VIM环境

    配置基于python的VIM环境 安装插件管理工具 为防止过多插件管理的麻烦,首先安装vim的插件管理工具Vundle.vundle本身的github软件已经有相关的中文文档,地址如下: vundle ...

  5. 1、Spring MVC的web.xml配置详解(转)

    版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/u010796790 1.spring 框架解决字符串编码问题:过滤器 CharacterEncodingFilt ...

  6. Vue 一些零零散散~

    1. F5刷新不会触发vue的destroyed事件. 2. computed 的 vuex 数据 ------>   beforeCreated: undefined   /    creat ...

  7. mongoose 常用数据库操作 删除

    删除 Model.remove(conditions, [callback]) try.js var User = require("./user.js"); function d ...

  8. hdu 3450 后缀数组

    题目大意: 求多个字符串的最长公共子串 基本思路: 参加我的博客hdu2774 代码如下: #include<cstdio> #include<cstring> using n ...

  9. 「题解」:$Six$

    问题 A: Six 时间限制: 1 Sec  内存限制: 512 MB 题面 题面谢绝公开. 题解 来写一篇正经的题解. 每一个数对于答案的贡献与数本身无关,只与它包含了哪几个质因数有关. 所以考虑二 ...

  10. 关于performSelector调用和直接调用区别

    下面两段代码都在主线程中运行,我们在看别人代码时会发现有时会直接调用,有时会利用performSelector调用,今天看到有人在问这个问题,我便做一下总结, [delegate imageDownl ...