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


相关推荐

  • 公网远程开机(唤醒家庭PC)

    公网远程开机(唤醒家庭PC)一、背景前一段搞使用seafile搞了私有云盘,不过不需要的时候开着电脑好像有点浪费,所以就开始了通过公网开机的道路二、关键性问题问题要说在前面,常规性的东西百度一般配置都可以搞定,如果各种面向百度的配置都已经尝试,请注意如下问题。. 一定在被开机的直连路由设备上进行MAC与IP地址绑定(原理略)。局域网直接子网内广播发送可以不用绑定。 被开机的系统需要安装对应的网卡驱动(实验CentOS7是有问题的,windows用驱动精灵安装下网卡驱动搞定)三、通过互联网公网远程开机一般性步骤按照常

    2022年5月30日
    67
  • 超级用户权限root_小米开发版root权限获取

    超级用户权限root_小米开发版root权限获取小米手机6X有没有办法开启ROOT超级权限?我们知道,安卓手机有ROOT超级权限,如果手机开启root相关权限,能够实现更好的功能,举例子,我们部门的营销部门,使用一些营销软件都需要在ROOT超级权限下执行,如果手机没办法获的root的权限,即没办法正常使用具体的功能。小米手机6X开发版系统自身拥有root权限管理工具,如果你使用的是小米手机6X稳定版,建议可以先将小米手机6X刷入开发版,再进…

    2025年6月18日
    5
  • px像素和dp像素密度区别[通俗易懂]

    px像素和dp像素密度区别[通俗易懂]px即像素,1px代表屏幕上一物理像素点。dp(dip)Densityindependentpixels.设备无关像素,与像素密度相关。像素密度:每英寸包涵的像素数

    2022年5月25日
    48
  • ESP8266简介:三种编程方式「建议收藏」

    ESP8266简介:三种编程方式「建议收藏」随着互联网的日益发展,智能家居的观念也逐渐深入人心。想要玩转智能家居,就离不开互联网,今天给大家介绍一款模块——ESP8266。小编将手把手教大家利用8266实现家电的控制。ESP8266可以用来做串口透传,PWM调控,远程控制开关:控制插座、开关、电器等。该模块有三种工作模式,大家可以根据自己的具体情况来选择:STA模式:ESP8266模块通过路由器连接互联网,手机或电脑通过互联网…

    2022年6月10日
    440
  • linux服务器开启snmp_snmp trap

    linux服务器开启snmp_snmp trap配置snmptrapvi/etc/snmp/snmptrap.conf,在文件最后添加如下agentAddressudp:127.0.0.1:161viewsystemonlyincluded.1.3.6.1.2.1.1viewsystemonlyincluded.1.3.6.1.2.1.25.1rocommunitycsg-tsgz991172.16.140.214-VsystemonlyrouserauthOnlyUsersysL

    2022年8月20日
    15
  • dedecms内容页调用图片集文档的图集图片

    dedecms内容页调用图片集文档的图集图片

    2021年9月19日
    37

发表回复

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

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