国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java Config下的Spring Test幾種方式實(shí)例詳解

Java Config下的Spring Test幾種方式實(shí)例詳解

2020-09-22 10:23Java教程網(wǎng) Java教程

這篇文章主要介紹了Java Config下的Spring Test方式實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下

Java Config 下的Spring Test方式

用了三種方式:

1.純手動(dòng)取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

需要引入相應(yīng)包:

?
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的話,并不會(huì)真正的去執(zhí)行數(shù)據(jù)庫的操作。

還有一種用法是@Spy,暫時(shí)不了解具體使用方式,待研究。

相比之下,還是spring-test標(biāo)準(zhǔn)一些。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

原文鏈接:http://www.cnblogs.com/juepei/p/4920369.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美国产精品一区 | 成人国产精品 | 农村少妇kkkk7777 | 日韩欧美三级在线观看 | jlzzjlzz亚洲日本少妇 | 亚洲精品一区在线 | 一本久久综合亚洲鲁鲁五月天 | 久久国产精品久久久久久电车 | 午夜视频免费 | 中文字幕在线三区 | 国产成年人视频 | 亚洲色图88 | 黄色免费看 | 日韩精品一区二区三区四区五区 | 国产一区二区精品在线观看 | 成人在线视频一区 | 久久久91精品国产一区二区三区 | 免费的av网站 | 国产最新一区 | 中文字幕一区二区三区精彩视频 | www.狠狠干 | 一区二区高清 | 香蕉夜色 | 自拍在线 | 五月婷婷色网 | 亚洲第一成年人网站 | 亚洲精品欧美 | 久久免费精品视频 | 久久国产精品久久精品 | 亚洲一区中文字幕 | 中文字幕在线一区 | 亚洲永久免费 | av网站在线播放 | 国产精品美女久久久 | 毛片综合 | 欧美成人精品一区二区男人看 | 成年人免费看片 | 免费黄色大片 | 亚洲 欧美 综合 | 国产最新精品 | 国产一区二区欧美 |