激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - java spring整合junit操作(有詳細的分析過程)

java spring整合junit操作(有詳細的分析過程)

2020-08-18 00:28光哥_帥 Java教程

這篇文章主要介紹了java spring整合junit操作(有詳細的分析過程),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

此博客解決了什么問題:

解決測試的時候代碼冗余的問題,解決了測試工程師的編碼能力可能沒有開發工程師編碼能力的問題,解決了junit單元測試和spring注解相結合!

測試類代碼:(只給大家展示測試類的代碼)

java" id="highlighter_731098">
?
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
public class AccountServiceTest {
 @Test
 public void testFindAll(){
  //1.獲取容器
  ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
  //2.得到業務層對象
  IAccountService as =ac.getBean("accountService",IAccountService.class);
  //3.執行方法
   List<Account> accounts=as.findAllAccount();
   for(Account account:accounts){
    System.out.println(account);
   }
 }
 
 @Test
 public void testFindSave(){
  Account account=new Account();
  account.setMoney(20000f);
  account.setName("test");
  //1.獲取容器
  ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
  //2.得到業務層對象
  IAccountService as =ac.getBean("accountService",IAccountService.class);
  as.saveAccount(account);
 }
 @Test
 public void testFindUpdate(){
  Account account=new Account();
 
  //1.獲取容器
  ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
  //2.得到業務層對象
  IAccountService as =ac.getBean("accountService",IAccountService.class);
  account=as.findAccountById(4);
   account.setMoney(40000f);
  as.updateAccount(account);
 }
}

以上的代碼都有公共的地方:

?
1
2
3
4
//1.獲取容器
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//2.得到業務層對象
IAccountService as =ac.getBean("accountService",IAccountService.class);

此時為了減少代碼的冗余我們完全可以將其抽離出來,如下:

?
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
private ApplicationContext ac;
private IAccountService as;
 
@Before
public void init(){
 //1.獲取容器
  ac=new ClassPathXmlApplicationContext("bean.xml");
 //2.得到業務層對象
  as =ac.getBean("accountService",IAccountService.class);
}
 
@Test
public void testFindAll(){
 
 //3.執行方法
  List<Account> accounts=as.findAllAccount();
  for(Account account:accounts){
   System.out.println(account);
  }
}
@Test
public void testFindSave(){
 Account account=new Account();
 account.setMoney(20000f);
 account.setName("test");
 as.saveAccount(account);
}
@Test
public void testFindUpdate(){
 Account account=new Account();
 account=as.findAccountById(4);
  account.setMoney(40000f);
 as.updateAccount(account);
}

上面的代碼似乎解決了我們的問題,但是我們忽略了一個問題,就是說在軟件開發的過程中,這是兩個角色,開發代碼的是軟件開發工程師,而這個測試的為軟件測試工程師,對于測試人員只管方法能不能執行,性能怎么樣,上面抽離出的代碼測試人員不一定會寫!

?
1
2
3
4
5
6
7
8
9
10
private ApplicationContext ac;
private IAccountService as;
 
@Before
public void init(){
 //1.獲取容器
  ac=new ClassPathXmlApplicationContext("bean.xml");
 //2.得到業務層對象
  as =ac.getBean("accountService",IAccountService.class);
}

分析:

首先我們先明確三點:

1.一般應用程序的入口都有main方法,但是在junit單元測試中,沒有main方法也能執行,junit集成了一個main方法,該方法就會判斷當前測試類中 是否有@test注解,然后讓帶著Test注解的類執行。

2、junit不會管我們是否采用spring框架,在執行測試方法時,junit根本不知道我們是不是使用了spring框架,所以也就不會為我們讀取配置文件/配置類創建spring核心容器

3.當測試方法執行時,沒有Ioc容器,就算寫了Autowired注解,也無法實現注入

綜上所述:按照我們之前的Autowried注入已經不好使了!接下看解決辦法:

1.導入spring整合junit的jar(坐標)

?
1
2
3
4
5
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-test</artifactId>
 <version>5.0.2.RELEASE</version>
</dependency>

2.使用junit提供的一個注解把原有的main方法替換了,替換成spring提供的,

這個注解是@RunWith,然后網上有這樣的解釋,我覺得比較貼切:

@RunWith就是一個運行器

@RunWith(JUnit4.class)就是指用JUnit4來運行

@RunWith(SpringJUnit4ClassRunner.class),讓測試運行于Spring測試環境,以便在測試開始的時候自動創建Spring的應用上下文

注解了@RunWith就可以直接使用spring容器,直接使用@Test注解,不用啟動spring容器

@RunWith(Suite.class)的話就是一套測試集合

3.告知spring的運行器,spring創建是基于xml還是注解的,并說明位置

這個注解就是:@ContextConfiguration

locations:指定xml文件的位置,加上classpath關鍵字,表示在類路徑下

classes: 指定注解類所在地位置

當我們使用spring 5.x版本的時候,要求junit的jar必須是4.12及以上

?
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
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
 
public class AccountServiceTest {
 @Autowired
 private IAccountService accountService;
 
 @Test
 public void testFindAll() {
  //3.執行方法
  List<Account> accounts = accountService.findAllAccount();
  for(Account account : accounts){
   System.out.println(account);
  }
 }
 
 @Test
 public void testSave() {
  Account account = new Account();
  account.setName("test anno");
  account.setMoney(12345f);
  //3.執行方法
  accountService.saveAccount(account);
 }
 
 @Test
 public void testUpdate() {
  //3.執行方法
  Account account = accountService.findAccountById(4);
  account.setMoney(23456f);
  accountService.updateAccount(account);
 }
}

補充知識:idea Could not autowire. No beans of 'XXXX' type found.

如下圖:在使用@Autowired注解的時候,提示找不到bean類型,查找了半天錯誤,發現這就不是錯誤,因為它根本不會影響程序的運行! 此時我以為是我的Service層注解沒寫,可是明明寫了!看下面的解決辦法!

java spring整合junit操作(有詳細的分析過程)

解決辦法:

點擊文件–setting–Editor–Inspections–spring–Warning–Apply–OK

java spring整合junit操作(有詳細的分析過程)

以上這篇java spring整合junit操作(有詳細的分析過程)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/jerry11112/article/details/97486927

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 92自拍视频 | 在线a亚洲视频播放在线观看 | 国产噜噜噜 | 视频一区二区三区中文字幕 | 欧美亚洲另类在线 | 欧美性色生活片免费播放 | 91 视频网站 | 九草在线视频 | 中文亚洲视频 | 午夜爽爽爽男女免费观看hd | 国产影院在线观看 | 海外中文字幕在线观看 | 日韩a毛片免费观看 | 久久久久久三区 | 舌头伸进添的我好爽高潮网站 | 久久国产亚洲精品 | 杏美月av| 一级片a| 亚洲午夜一区二区三区 | 久久不射电影 | 国产艳妇av视国产精选av一区 | 超碰97最新 | 久久精品在线免费观看 | 一区二区三区在线观看免费视频 | 成人免费毛片在线观看 | 国产精品亚洲yourport | 亚洲一区二区三区四区精品 | 热99精品视频 | 美女视频黄a视频免费全过程 | 9999久久久久久| 成年人在线视频观看 | lutube成人福利在线观看污 | 免费黄色日韩电影 | 一级免费看片 | 乱淫67194 | 49vv看片免费 | 久久精品视频免费 | 女人解衣喂奶电影 | 成年人国产视频 | 国产一区免费观看 | 成人国产免费观看 |