用了三种方式:
1.纯手动取bean:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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
需要引入相应包:
?
1
2
3
4
5
6
|
< dependency >
< groupId >org.mockito</ groupId >
< artifactId >mockito-all</ artifactId >
< version >1.9.5</ version >
< scope >test</ scope >
</ dependency >
|
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
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标准一些。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://www.cnblogs.com/juepei/p/4920369.html
相关文章
猜你喜欢
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 23
-
2025-05-25 72
-
2025-05-29 97
-
操作系统迁移难?Alibaba Cloud Linux 支持跨版本升级 | 龙蜥技术
2025-05-26 78 -
IntelliJ IDEA2025.2.2创建Servlet方法及404问题
2025-05-29 89
热门评论