时间:2021-05-20
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标准一些。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Kotlin基础语法实例详解包定义和引入Java一样,在文件开头,行结束不需要”;“packagecom.test.helloimportandroid.os.
在上篇文章给大家介绍了Spring学习笔记1之IOC详解尽量使用注解以及java代码,接下来本文重点给大家介绍Spring学习笔记2之表单数据验证、文件上传实例
本文承接上一篇文章《Java多线程实例详解(一)》。四.Java多线程的阻塞状态与线程控制上文已经提到Java阻塞的几种具体类型。下面分别看下引起Java线程阻
使用Java的方式配置Spring我们现在要完全不使用Spring的xml配置,全权使用Java来配置Spring!JavaConfig是Spring的一个子项
@Profile注解详解@Profile:Spring为我们提供的可以根据当前环境,动态的激活和切换一系列组件的功能;开发环境develop、测试环境test、