用了三种方式:

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标准一些。

Java Config 下的Spring Test方式的更多相关文章

  1. Spring 4 and MyBatis Java Config

    TL;DR With the Java Config enhancements in Spring 4, you no longer need xml to configure MyBatis for ...

  2. 【linux】linux查看资源任务管理器,使用top命令 + 查看java进程下的线程数量【两种方式】

    ================================ 详解:https://blog.csdn.net/achenyuan/article/details/77867661 ======= ...

  3. Spring Boot 获取 java resources 下文件

    Spring Boot 获取 java resources 下文件 Spring Boot 获取 resources 目录下的目录(例:获取 resources 目录下的 template 目录): ...

  4. Spring MVC 的 Java Config ( 非 XML ) 配置方式

    索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml web/pom.xml web.xml WebInitializer.java ...

  5. spring、springmvc和mybatis整合(java config方式)

    之前项目中使用ssm框架大多是基于xml的方式,spring3.0以后就提供java config的模式来构建项目,并且也推荐使用这种方式,自从接触过springboot后,深深感受到这种纯java配 ...

  6. Spring Security4实例(Java config 版) —— Remember-Me

    本文源码请看这里 相关文章: Spring Security4实例(Java config版)--ajax登录,自定义验证 Spring Security提供了两种remember-me的实现,一种是 ...

  7. Spring Web工程web.xml零配置即使用Java Config + Annotation

    摘要: 在Spring 3.0之前,我们工程中常用Bean都是通过XML形式的文件注解的,少了还可以,但是数量多,关系复杂到后期就很难维护了,所以在3.x之后Spring官方推荐使用Java Conf ...

  8. spring java config 初探

    Java Config 注解 spring java config作为同xml配置形式的另一种表达形式,使用的场景越来越多,在新版本的spring boot中 大量使用,今天我们来看下用到的主要注解有 ...

  9. 你真的懂Spring Java Config 吗?Full @Configuration vs lite @Bean mode

    Full @Configuration和lite @Bean mode 是 Spring Java Config 中两个非常有意思的概念. 先来看一下官方文档关于这两者的相关内容: The @Bean ...

随机推荐

  1. 使用fastcgi_finish_request提高页面响应速度

    当PHP运行在FastCGI模式时,PHP FPM提供了一个名为fastcgi_finish_request的方法. 按照文档上的说法,此方法可以提高请求的处理速度,如果有些处理可以在页面生成完后再进 ...

  2. QT 使用jsoncpp

    QT 使用jsoncpp 编译jsoncpp 编译前先安装好python,scons,解压jsoncpp到目录e:\jsconcpp,查看目录下的readme,有关于编译的说明的,根据说明做相应操作就 ...

  3. Java制作证书的工具keytool用法总结

    一.keytool的概念 keytool 是个密钥和证书管理工具.它使用户能够管理自己的公钥/私钥对及相关证书,用于(通过数字签名)自我认证(用户向别的用户/服务认证自己)或数据完整性以及认证服务.在 ...

  4. Struts2复习笔记

    一.Struts2的获得 Struts2的官网是http://struts.apache.org/,需要的同学可以自己去官网上下载. 二.Struts2的配置 以动态web项目为例,讲解配置Strut ...

  5. 技术英文单词贴--B

    B breadcrumb 面包屑 bubble 冒泡

  6. linux中的开机和关机命令

    与关机.重新启动相关的命令 * 将数据同步写入硬盘中的命令  sync * 惯用的关机命令  shutdown * 重新启动.关机  reboot halt poweroff sync 强制将内存中的 ...

  7. unreal3的坐标系统和vector/rotator

    unreal3的坐标系有点奇葩,属于[z轴向上的左手坐标系]: 1.左手食指指前方,x正向 2.大拇指指上方,z正向 3.中指指右方,y正向 若以我们人头摆正时来参考: 1.目视的是前方,x正向 2. ...

  8. Jenkins 笔记

    1.Jenkins是什么? 他是一个开源的自动化服务器,持续集成工具.由Java和上百个插件组成,支持编译,测试,部署任意的自动化项目. 2.怎么安装Jenkins? 方法一:从官网 https:// ...

  9. easyui combotree 默认 初始化时就选中

    做得权限管理系统,用combotree来控制权限,combotree是通过后台json来初始化,但是前台不是选中状态. 下面是核心代码已经标红. $('#txtTree').combotree({ m ...

  10. Oracle 游标使用

    今天看到一篇很帮的游标,所以强烈转一下 http://www.cnblogs.com/sc-xx/archive/2011/12/03/2275084.html -- 声明游标:CURSOR curs ...