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


相关推荐

  • html div里加滚动条,给DIV添加滚动条

    html div里加滚动条,给DIV添加滚动条直接为div指定overflow属性为auto即可,但是必须指定div的高度,如下:判断div是否有滚动条判断div是否有滚动条varobj=document.getElementById(“showDiv”);if(obj.scrollHeight要用两个div配合实现http://www.lao8.org/html/8/2009-6-9/div_css/div显示滚动条的css代码无标题文档…

    2022年7月14日
    14
  • ElasticSearch数据库(ES数据库)简介

    ElasticSearch数据库(ES数据库)简介一elasticsearch简介**ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTfulweb接口。**Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。1elasticSearch的使用场景1、为用户提供按关键字查询的全文搜索功能。2、实现…

    2022年4月28日
    81
  • 微信开放平台网站应用扫码登录

    微信开放平台网站应用扫码登录<?phpnamespaceapp\common\wechat;/***微信开放平台*Date:2020/9/080015*Time:上午10:03*/classWXOpenPlatform{privatestatic$appId=’xxxxxxxxxxxxxxxxxxxxxxxxx’;privatestatic$appSecret=’xxxxxxxxxxxxxxxxxxxxxxxxx’;privatestati.

    2022年5月26日
    36
  • ringbuffer的常规用法_ring up

    ringbuffer的常规用法_ring up自己的slam车关于ros和stm32控制板的通信部分,之前一直使用rosserial_python包,前段时间重新编写了上、下位机的通信程序,本篇只对数据的RingBuffer做点总结。由于是用于串口的帧数据传输,为保证每帧数据完整性,采用字节入队和出队的方式实现,测试结果也比较稳定。classRingBuffer{      public:      RingBuff…

    2022年9月10日
    0
  • 免费ASP空间

    免费ASP空间free免费asp空间的申请方法;free免费asp空间的ftp使用方法;sise.host3v.vip/1

    2022年7月2日
    19
  • matlab 折线图 标记_matlab画折线图标记线

    matlab 折线图 标记_matlab画折线图标记线…’MarkerSize’,10)xlabel(‘x’);ylabel(‘y’);·用Matlab画图时,有时候需要对各种图标进行标注,例如,用“+”代表A的运动情况,“*”代表……画出来就成了折线图,请试验之(*);(,’:’,”)同时画两个函数若要改变颜色,在座标对后面加上相关字串即可:;((),”)若要同时改变颜色及图线……是用m…

    2022年4月30日
    40

发表回复

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

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