Spring对junit的整合
2024-09-01 13:32:54
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); } }
最新文章
- Yii 1开发日记 -- 搜索功能及Checkbox的实现
- iOS程序中的内存分配 栈区堆区全局区
- Surface与SurfaceView、SurfaceHolder
- Python + OpenCV2 系列:2 - 图片操作
- caffe学习系列(7):Blob,layer,Net介绍
- 9.27js拓展、bootstrap菜鸟教程
- URL List
- 2015第42周六Pgsql全文索引
- 黑龙江省第七届大学生程序设计竞赛-Mysterious Organization
- Bitmap与Matrix旋转ImageView
- [转]ubuntu搭建LAMP环境
- Interlocked原子函数陷阱
- Net Core Docker 容器部署,修改,保存
- Vue项目用了脚手架vue-cli3.0,会报错You are using the runtime-only build of Vue where the template compiler is not available.....
- 二分图匹配-HK算法
- 【 js 基础 】为什么 call 比 apply 快?
- 一分钟理清Vue-cli 代码构建步骤。
- SQLServer转MYSQL的方法(连数据)[传]
- swift中Double转String
- centos6.8上安装paramiko
热门文章
- [已解决]报错:have mixed types. Specify dtype option on import or set low_memory=False
- springboot Service层单元测试
- 正在从 Windows 应用商店下载... 无法从 Windows 应用商店下载。请检查网络连接。
- typescript 类型映射 (ReadOnly、Partial)
- Cas简介(一)
- wordpress 添加 显示磁盘剩余空间百分比的插件
- (微服务架构)Security + Oauth2 + Jwt + Zuul解决微服务系统的安全问题
- luoguP2709 小B的询问 [莫队]
- luoguP2148 [SDOI2009]E&;D [sg函数][组合游戏]
- mongodb导入csv