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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • shift work什么意思_shift delete什么意思

    shift work什么意思_shift delete什么意思shift后门之管理员的用处『罗斌原创』shift后门,网络/系统管理员如果想用的话,那还是可以的,为什么要用它呢,第一,公司大部分的电脑都没有光驱,有的甚至不支持从U盘启动,当一个员工走了之后,而他的电脑又设置了密码,无从下手激活成功教程密码,除非把它的电脑拆了,把硬盘挂在其他电脑上去激活成功教程。或者重装系统!这样是不是非常麻烦呢?第…

    2025年12月1日
    4
  • phpstorm激活码2021【注册码】

    phpstorm激活码2021【注册码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    32
  • idea2022.01 激活码【中文破解版】2022.03.07

    (idea2022.01 激活码)JetBrains旗下有多款编译器工具(如:IntelliJ、WebStorm、PyCharm等)在各编程领域几乎都占据了垄断地位。建立在开源IntelliJ平台之上,过去15年以来,JetBrains一直在不断发展和完善这个平台。这个平台可以针对您的开发工作流进行微调并且能够提供…

    2022年4月2日
    76
  • 爬虫(2)之re 爬取淘宝网「建议收藏」

    爬虫(2)之re 爬取淘宝网「建议收藏」我们通过requests可以很轻松地就获得网页上的所有内容,但是这些内容往往会夹杂着许多我们不需要的东西,因此我们需要解析和提取HTML数据。在先前介绍过的解析和提取html内容的库,只能够处理静态文本执行简单的搜索,缺乏灵活性,不能处理动态的文本信息。下面来介绍一下正则表达式。什么是正则表达式?正则表达式是用来简洁表达一组字符串的表达式正则表达式是一种通用的字符串表达框架正则表…

    2022年6月18日
    34
  • Qt —— QWebEngineView加载谷歌离线地图(包含离线地图瓦片下载制作)

    Qt —— QWebEngineView加载谷歌离线地图(包含离线地图瓦片下载制作) 关注微信公众号搜索”Qt_io_”或”Qt开发者中心”了解更多关于Qt、C++开发知识.。笔者-jxd

    2022年9月20日
    7
  • idea卸载删除旧版重新安装新版后,新版本idea程序打不开闪退的解决方案

    idea卸载删除旧版重新安装新版后,新版本idea程序打不开闪退的解决方案一般情况下,都是因为激活成功教程idea时,在启动参数配置文件idea64.exe.vmoptions中添加了如下参数:-javaagent:D:\IntelliJIDEA2020.1\bin\jetbrainsCrack.jar指定了一个jar文件,而新的idea会复制该配置文件,拿来直接使用,但是如果这个jar文件不存在,那么新安装的idea就打不开。解决方案需要将idea的idea64.exe.vmoptions配置文件中上面那个参数删除掉即可,但idea64.exe.vmoptions配置

    2022年6月18日
    253

发表回复

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

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