java motherfree video_Java Config 下的Spring Test方式
用了三种方式:
1.纯手动取bean:
package com.wang.test;
import com.marsmother.commission.core.config.MapperConfig;
import com.marsmother.commission.core.config.PropertyConfig;
import com.marsmother.commission.core.config.ServiceConfig;
import com.marsmother.commission.core.dto.GeneralResponseData;
import com.marsmother.commission.core.service.UserService;
import com.marsmother.commission.site.config.SecurityConfig;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by Wanglei on 15/10/29.
*/
public class CustomeTest {
private static AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@Before
public void tearUp(){
context.register(PropertyConfig.class);
context.register(ServiceConfig.class);
context.register(SecurityConfig.class);
context.register(MapperConfig.class);
context.refresh();
}
@Test
public void testUser(){
UserService userService = context.getBean(UserService.class);
Long userId = 3L;
GeneralResponseData data = userService.addUserRelation(userId);
System.out.println(data.getMsg());
}
}
2.采用spring-test
package com.wang.test;
import com.marsmother.commission.core.config.MapperConfig;
import com.marsmother.commission.core.config.PropertyConfig;
import com.marsmother.commission.core.config.ServiceConfig;
import com.marsmother.commission.core.dto.GeneralResponseData;
import com.marsmother.commission.core.service.UserService;
import com.marsmother.commission.site.config.SecurityConfig;
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;
/**
* Created by Wanglei on 15/10/29.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {PropertyConfig.class, ServiceConfig.class, SecurityConfig.class, MapperConfig.class})
public class SpringTest {
@Autowired
private UserService userService;
@Test
public void testUser(){
GeneralResponseData data= userService.addUserRelation(3L);
System.out.println(data.getMsg());
}
}
3.采用Mockito
需要引入相应包:
org.mockito
mockito-all
1.9.5
test
package com.wang.test;
import com.marsmother.commission.core.dto.GeneralResponseData;
import com.marsmother.commission.core.presistence.FollowNumberMapper;
import com.marsmother.commission.core.presistence.UserMapper;
import com.marsmother.commission.core.presistence.UserRelationMapper;
import com.marsmother.commission.core.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
/**
* Created by Wanglei on 15/10/29.
*/
public class TestUserService {
@InjectMocks
private UserService userService;
@Mock
private FollowNumberMapper followNumberMapper;
@Mock
private UserMapper userMapper;
@Mock
private UserRelationMapper userRelationMapper;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
}
@Test
public void testUser(){
Long userId = 3L;
GeneralResponseData result = userService.addUserRelation(userId);
System.out.println(result.getMsg());
}
}
这里@Mock的话,并不会真正的去执行数据库的操作。
还有一种用法是@Spy,暂时不了解具体使用方式,待研究。
相比之下,还是spring-test标准一些。
java motherfree video_Java Config 下的Spring Test方式相关推荐
- [Java,MVC] Eclipse下搭建Spring MVC
转自:http://blog.csdn.net/blue_jjw/article/details/8752466 一.新建Dynamic Web Project 一个web工程最基本的,只看3个地方, ...
- JNDI(datasource)在tomcat,JBOSS下的spring+quartz配置
由于项目中多个小应用(类似插件)需要使用数据库连接池,不能为每一个应用中配置连接池.故采用JNDI的模式进行配置. 我们使用的web服务器有tomcat和jboss两种.分别介绍其在这两种服务器下并且 ...
- java布道师_我和 Spring 技术布道师的一天
摘要: 先介绍一下故事的5位主人公. Josh Long 龙之春:Spring 技术布道师,撰写过5部著作,录制过3部畅销的培训视频,是一位开源软件贡献者. Spencer Gibb:Spring 技 ...
- spring boot 外部配置文件1-spring.config.location与spring.config.additional-location的区别
spring boot外部配置文件 基础环境 : 基础环境 版本 spring boot 2.7.0 JDK 8 测试代码: @SpringBootApplication public class D ...
- JAVA面试汇总第四章 Spring及数据库相关
Spring 核心功能演示 + 面试题 Spring Framework 简称 Spring,是 Java 开发中最常用的框架,地位仅次于 Java API,就连近几年比较流行的微服务框架 Sprin ...
- 【Java学习路线之JavaWeb】Spring Cloud教程(非常详细)
文章目录 读者 阅读条件 微服务是什么 微服务,我们可以从字面上去理解,即"微小的服务",下面我们从"服务"和"微小"两个方面进行介绍. 微 ...
- effective java英文版pdf_Java之Spring1:Spring简介、环境搭建、源码下载及导入MyEclipse...
框架学习前言 这个模块是面向Spring的,Spring的学习我是这么想的: 1.简单介绍Spring,主要是从网上借鉴一些重点 2.尽量说明清楚Spring的使用方法以及细节点 3.尽量以自己的理解 ...
- Java面试中常问的Spring方面问题
1.一般问题 1.1. 不同版本的 Spring Framework 有哪些主要功能? VersionFeatureSpring 2.5发布于 2007 年.这是第一个支持注解的版本.Spring 3 ...
- Java应用集群下的定时任务处理方案(mysql)
今天来说一个Java多机部署下定时任务的处理方案. 需求: 有两台服务器同时部署了同一套代码, 代码中写有spring自带的定时任务,但是每次执行定时任务时只需要一台机器去执行. 当拿到这个需求时我脑 ...
最新文章
- UGUI_判断鼠标或者手指是否点击在UI上
- leetcode1046. 最后一块石头的重量(堆)
- 牛客16585 统计单词数
- 编译期java_java编译期和运行期
- 《Python编程从入门到实践》记录之while循环简介(break、continue)
- 同TTX更可爱的层次分析法游戏破解
- 学习笔记:App-V测试错误代码4505CD-1690150A-20000194
- 团队项目前期冲刺-7
- 【STM32训练—SIM900A模块】第一篇、电脑的串口助手驱动SIM900A发送中文和英文短信
- 计算机如何将两个磁盘合在一起,win10怎么把电脑自带的两个磁盘合并到一起
- 谷歌内核浏览器无法下载文件的解决方法
- 电子书免费下载及文本格式转换问题
- jenkins 并发构建Android 报错:Gradle build daemon disappeared unexpectedly (it may have been killed or may
- 关于浏览器自动转https
- 计算机论文 a会 c会,ccf b类论文 sci几区_焦文静_电影学者王田
- 纳人才,谋发展 | 人大金仓喜获“最佳雇主校招案例奖”
- int int int * int**的区别、联系和用途
- 使用Python处理声音文件(三):剪辑与淡入淡出
- UML一篇文章就学通!
- 网站识别Windows AD 域账号,并自动登录