java motherfree video_Java Config 下的Spring Test方式

java motherfree video_Java Config 下的Spring Test方式用了三种方式:1.纯手动取bean:packagecom.wang.test;importcom.marsmother.commission.core.config.MapperConfig;importcom.marsmother.commission.core.config.PropertyConfig;importcom.marsmother.commission.core.conf…

大家好,又见面了,我是你们的朋友全栈君。

用了三种方式:

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/156603.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux安装svn服务器的两种方式

    linux安装svn服务器的两种方式操作环境:CentOS7.264位==============第一种:采用压缩包安装:比如你们公司服务器上安装软件有自己的规定,一般会采用压缩包安装。==============第一步:SVN服务器端压缩包下载下载地址:http://mirrors.cnnic.cn/apache/subversion/ 我下载的是subversion-1.8.19版本,下载好的压缩包置于目录/usr/loc…

    2022年7月19日
    12
  • 噪声熵和损失熵的定义_归一化信噪比

    噪声熵和损失熵的定义_归一化信噪比利用K-L散度(相对熵)确定VMD分解信号的K值和惩罚因子alpha,得到一组信号分量;计算各个分量的样本熵,根据样本熵的值,选取出噪声主导分量和有效分量;对噪声主导信号进行非局部均值(NLM)去噪;将去噪后的信号分量与剩余的有效信号分量进行重构得到去噪信号。

    2025年6月8日
    0
  • Execution failed for task ‘:sdkdemo:mergeDebugResources‘.

    Execution failed for task ‘:sdkdemo:mergeDebugResources‘.

    2021年10月2日
    37
  • tp框架的特性_tp5框架

    tp框架的特性_tp5框架一、结构目录》Thinkphp文件夹,是thinkPHP的核心文件,里面的内容是不允许我们修改的》Public是公有的文件夹,里面可以写自己的东西》Application我们建立项目的文件夹(Public和Application里面原有自带的文件可以删除,相当于两个空的文件夹)》tp文件夹中的index.php是所有程序的入口文件》conf->convention.php配置文件包含了所有的配置》thinkphp->conf文件夹->配置文件,几乎包括关于..

    2025年5月25日
    1
  • 个人网站如何赚钱

    个人网站如何赚钱

    2021年9月9日
    79
  • 超详细!ActionBar使用详解

    转自:https://www.cnblogs.com/mjsn/p/6150824.html一、ActionBar介绍  在Android3.0中除了我们重点讲解的Fragment外,ActionBar也是一个非常重要的交互元素,ActionBar取代了传统的tittlebar和menu,在程序运行中一直置于顶部,对于Android平板设备来说屏幕更大它的标题使用Action…

    2022年4月18日
    131

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号