用了三种方式:

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. Apache(ApacheHaus)安装配置教程

    1,Apache下载 选择一个版本,点击Download 点击File For Microsoft Windows 由于Apache HTTP Server官方不提供二进制(可执行)的发行版,所以我们 ...

  2. objective c,copy, mutableCopy区别

    copy总是返回不能被修改的对象,mutableCopy返回可以被修改的对象 例: NSArray *array = @[@"test", @"test2"]; ...

  3. Cadence UVM基础视频介绍(UVM SV Basics)

    Cadence关于UVM的简单介绍,包括UVM的各个方面.有中文和英文两种版本. UVM SV Basics 1 – Introduction UVM SV Basics 2 – DUT Exampl ...

  4. compile error

    stray \241     程序有非法字符,如空格,引号等,一般因为从别的地方粘贴导致这个错误.

  5. C# random生成随机数全部一样

    最近做排序测试  使用random生成随机数全部一样 估计是因为random采用的随机种子为时间戳 而一个循化执行消耗的时间没有到时间戳的最小单位 故没有变化 Thread.Sleep(10); 使用 ...

  6. php 图片处理类

    <?php /** * 图片类 * @author <420012223@qq.cn> */ class Image { public $uploadImagePath = './t ...

  7. 你必须了解的基础的 Linux 网络命令

    Linux 基础网络命令列表 我在计算机网络课程上使用 FreeBSD,不过这些 UNIX 命令应该也能在 Linux 上同样工作. 连通性 ping <host>:发送 ICMP ech ...

  8. C++11 删除链表重复数值

    #include <memory> #include <iostream> #include <chrono> #include <thread> us ...

  9. Uber从Postgres切换到MySQL

    Uber工程师在官方博客上描述了他们为什么要从 Postgres 切换到 MySQL 数据库.Uber的早期架构是由 Python编写的后端应用构成,使用了 Postgres 数据库.但此后,Uber ...

  10. 20145225《Java程序设计》 第5周学习总结

    20145225<Java程序设计> 第5周学习总结 教材学习内容总结 第八章 异常处理 8.1语法与继承架构 try.catch:try.catch代表错误的对象后做一些处理. 异常继承 ...