Java Config下的Spring Test几种方式实例详解

 更新时间:2017年05月02日 11:57:48   投稿:lqh  
这篇文章主要介绍了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标准一些。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • 浅析Java中关键词volatile底层的实现原理

    浅析Java中关键词volatile底层的实现原理

    在 Java 并发编程中,有 3 个最常用的关键字:synchronized、ReentrantLock 和 volatile,这篇文章主要来和大家聊聊volatile底层的实现原理,感兴趣的可以了解下
    2024-02-02
  • Java实现斗地主最简代码实例

    Java实现斗地主最简代码实例

    在本篇文章里小编给各位分享的是关于Java实现斗地主最简代码实例,有兴趣的朋友们可以参考下。
    2020-05-05
  • java实现马踏棋盘的完整版

    java实现马踏棋盘的完整版

    这篇文章主要为大家详细介绍了java实现马踏棋盘的完整版,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Elasticsearch模糊查询详细介绍

    Elasticsearch模糊查询详细介绍

    这篇文章主要给大家介绍了关于Elasticsearch模糊查询的相关资料,在数据库查询中模糊查询是一种强大的技术,可以用来搜索与指定模式匹配的数据,需要的朋友可以参考下
    2023-09-09
  • 使用Vert.x Maven插件快速创建项目的方法

    使用Vert.x Maven插件快速创建项目的方法

    这篇文章主要介绍了使用Vert.x Maven插件快速创建项目的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • Java StringBuilder类原理及常用方法

    Java StringBuilder类原理及常用方法

    这篇文章主要介绍了Java StringBuilder类原理及常用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • 如何使用SpringMVC的消息转换器设置日期格式

    如何使用SpringMVC的消息转换器设置日期格式

    这篇文章主要介绍了如何使用SpringMVC的消息转换器设置日期格式问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-07-07
  • SpringCloud Gateway的路由,过滤器和限流解读

    SpringCloud Gateway的路由,过滤器和限流解读

    这篇文章主要介绍了SpringCloud Gateway的路由,过滤器和限流解读,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • java使用common-httpclient包实现post请求方法示例

    java使用common-httpclient包实现post请求方法示例

    这篇文章主要给大家介绍了关于java使用common-httpclient包实现post请求的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-08-08
  • Spring MVC实现mysql数据库增删改查完整实例

    Spring MVC实现mysql数据库增删改查完整实例

    这篇文章主要介绍了Spring MVC实现mysql数据库增删改查完整实例,从创建一个web项目开始,分享了项目结构以及具体Java代码和前端页面等相关内容,具有一定借鉴价值,需要的朋友可以了解下。
    2017-12-12

最新评论