Java Config下的Spring Test几种方式实例详解
作者:lqh 发布时间:2022-12-17 00:31:36
标签:Java,Config,Spring,Test
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
需要引入相应包:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
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标准一些。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://www.cnblogs.com/juepei/p/4920369.html


猜你喜欢
- TableView,算是一个很重要的控件,几乎随处可见,而且功能强大,数据展示效果良好。所以,在JavaFX中,我们自然而然也应该学习一下T
- Android四种数据存储的应用方式作为一个完整的应用程序,数据存储操作是必不可少的。因此,Android系统一共提供了四种数据存储方式。分
- 我们在安装某个APP的时候,基本都会有一个引导页的提示,他们可以打广告,或者介绍新功能的加入和使用说明等。一般都支持滑动并且下面有几个点,显
- 1、简介单例模式使⽤场景:业务系统全局只需要⼀个对象实例,⽐如发号器、 redis 连接对象等。Spring IOC容器中的 Bean 默认
- 本文实例分析了C#双缓冲技术。分享给大家供大家参考,具体如下:双缓冲解决闪烁问题。整理:GDI+的双缓冲问题一直以来的误区:.net1.1
- C#中的null与SQL中的NULL是不一样的,SQL中的NULL用C#表示出来就是DBNull.Value。注意:SQL参数是不能接受C#
- 起源最近公司要做多租户,Mybatis-Plus的多租户插件很好用,但是有一个场景是:字典表或者某些数据表,一些数据需要在各个租户之间共享,
- 基本数据类型变量就是用来储存值而保留的内存位置。这就意味着当你创建一个变量时就会在内存中占用一定的空间。基于变量的数据类型,操作系统会进行内
- Menustrip控件是一个菜单栏控件,可以加载菜单栏,在很多系统中都有用到。本文介绍下该控件的一些属性的常用方式。在页面FormMain_
- 静默安装就是偷偷的把一个应用安装到手机上,就是屏蔽确认框,通过反射只能写个主要的代码,这个是在linux编译用到,因为静默安装需要调用系统服
- 前言文件的上传和下载都是基于io复制,只不过文件上传是浏览器向服务器发送报文文件下载是服务器向浏览器发送报文提示:以下是本篇文章正文内容,下
- 在Web的应用方面有js的插件实现自动完成(或叫智能提示)功能,但在WinForm窗体应用方面就没那么好了。TextBox控件本身是提供了一
- 判断有无虚拟按键(导航栏)现在很大一部分手机没有虚拟按键,一部分有。我们在做适配的时候可能会用到这方面的知识。例如:屏幕填充整个屏幕的时候,
- 定义定义了算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户。解决的问题在有多种算法相似的情况下,
- 项目中需要对一个点击事件进行下载操作,同时通过点击事件,已经可以从jsp页面获取到需要访问的URL和下载的文件名(数据库获取,jsp页面显示
- 本文实例讲述了C#中TreeView实现适合两级节点的选中节点方法。分享给大家供大家参考。具体如下:class TreeViewChecke
- 本文介绍C# lock关键字,C#提供了一个关键字lock,它可以把一段代码定义为互斥段(critical section),互斥段在一个时
- 本文实例讲述了python调用java模块SmartXLS和jpype修改excel文件的方法。分享给大家供大家参考。具体实现方法如下:#
- 实践过程效果代码public partial class Form1 : Form{ public Form1()
- 特别是针对循环或timer处理中需要在窗体控件显示数据时,因后台处理过度繁忙而出现没刷新或者假死现象时,可以使用Application.Do