java spring整合junit操作(有詳細(xì)的分析過(guò)程)
此博客解決了什么問(wèn)題:
解決測(cè)試的時(shí)候代碼冗余的問(wèn)題,解決了測(cè)試工程師的編碼能力可能沒(méi)有開(kāi)發(fā)工程師編碼能力的問(wèn)題,解決了junit單元測(cè)試和spring注解相結(jié)合!
測(cè)試類代碼:(只給大家展示測(cè)試類的代碼)
public class AccountServiceTest { @Test public void testFindAll(){ //1.獲取容器 ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); //2.得到業(yè)務(wù)層對(duì)象 IAccountService as =ac.getBean("accountService",IAccountService.class); //3.執(zhí)行方法 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.得到業(yè)務(wù)層對(duì)象 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.得到業(yè)務(wù)層對(duì)象 IAccountService as =ac.getBean("accountService",IAccountService.class); account=as.findAccountById(4); account.setMoney(40000f); as.updateAccount(account); } }
以上的代碼都有公共的地方:
//1.獲取容器 ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml"); //2.得到業(yè)務(wù)層對(duì)象 IAccountService as =ac.getBean("accountService",IAccountService.class);
此時(shí)為了減少代碼的冗余我們完全可以將其抽離出來(lái),如下:
private ApplicationContext ac; private IAccountService as; @Before public void init(){ //1.獲取容器 ac=new ClassPathXmlApplicationContext("bean.xml"); //2.得到業(yè)務(wù)層對(duì)象 as =ac.getBean("accountService",IAccountService.class); } @Test public void testFindAll(){ //3.執(zhí)行方法 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); }
上面的代碼似乎解決了我們的問(wèn)題,但是我們忽略了一個(gè)問(wèn)題,就是說(shuō)在軟件開(kāi)發(fā)的過(guò)程中,這是兩個(gè)角色,開(kāi)發(fā)代碼的是軟件開(kāi)發(fā)工程師,而這個(gè)測(cè)試的為軟件測(cè)試工程師,對(duì)于測(cè)試人員只管方法能不能執(zhí)行,性能怎么樣,上面抽離出的代碼測(cè)試人員不一定會(huì)寫!
private ApplicationContext ac; private IAccountService as; @Before public void init(){ //1.獲取容器 ac=new ClassPathXmlApplicationContext("bean.xml"); //2.得到業(yè)務(wù)層對(duì)象 as =ac.getBean("accountService",IAccountService.class); }
分析:
首先我們先明確三點(diǎn):
1.一般應(yīng)用程序的入口都有main方法,但是在junit單元測(cè)試中,沒(méi)有main方法也能執(zhí)行,junit集成了一個(gè)main方法,該方法就會(huì)判斷當(dāng)前測(cè)試類中 是否有@test注解,然后讓帶著Test注解的類執(zhí)行。
2、junit不會(huì)管我們是否采用spring框架,在執(zhí)行測(cè)試方法時(shí),junit根本不知道我們是不是使用了spring框架,所以也就不會(huì)為我們讀取配置文件/配置類創(chuàng)建spring核心容器
3.當(dāng)測(cè)試方法執(zhí)行時(shí),沒(méi)有Ioc容器,就算寫了Autowired注解,也無(wú)法實(shí)現(xiàn)注入
綜上所述:按照我們之前的Autowried注入已經(jīng)不好使了!接下看解決辦法:
1.導(dǎo)入spring整合junit的jar(坐標(biāo))
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.2.RELEASE</version> </dependency>
2.使用junit提供的一個(gè)注解把原有的main方法替換了,替換成spring提供的,
這個(gè)注解是@RunWith,然后網(wǎng)上有這樣的解釋,我覺(jué)得比較貼切:
@RunWith就是一個(gè)運(yùn)行器
@RunWith(JUnit4.class)就是指用JUnit4來(lái)運(yùn)行
@RunWith(SpringJUnit4ClassRunner.class),讓測(cè)試運(yùn)行于Spring測(cè)試環(huán)境,以便在測(cè)試開(kāi)始的時(shí)候自動(dòng)創(chuàng)建Spring的應(yīng)用上下文
注解了@RunWith就可以直接使用spring容器,直接使用@Test注解,不用啟動(dòng)spring容器
@RunWith(Suite.class)的話就是一套測(cè)試集合
3.告知spring的運(yùn)行器,spring創(chuàng)建是基于xml還是注解的,并說(shuō)明位置
這個(gè)注解就是:@ContextConfiguration
locations:指定xml文件的位置,加上classpath關(guān)鍵字,表示在類路徑下
classes: 指定注解類所在地位置
當(dāng)我們使用spring 5.x版本的時(shí)候,要求junit的jar必須是4.12及以上
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfiguration.class) public class AccountServiceTest { @Autowired private IAccountService accountService; @Test public void testFindAll() { //3.執(zhí)行方法 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.執(zhí)行方法 accountService.saveAccount(account); } @Test public void testUpdate() { //3.執(zhí)行方法 Account account = accountService.findAccountById(4); account.setMoney(23456f); accountService.updateAccount(account); } }
補(bǔ)充知識(shí):idea Could not autowire. No beans of 'XXXX' type found.
如下圖:在使用@Autowired注解的時(shí)候,提示找不到bean類型,查找了半天錯(cuò)誤,發(fā)現(xiàn)這就不是錯(cuò)誤,因?yàn)樗静粫?huì)影響程序的運(yùn)行! 此時(shí)我以為是我的Service層注解沒(méi)寫,可是明明寫了!看下面的解決辦法!
解決辦法:
點(diǎn)擊文件–setting–Editor–Inspections–spring–Warning–Apply–OK
以上這篇java spring整合junit操作(有詳細(xì)的分析過(guò)程)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vscode 配置java環(huán)境并調(diào)試運(yùn)行的詳細(xì)過(guò)程
這篇文章主要介紹了vscode 配置java環(huán)境并調(diào)試運(yùn)行的詳細(xì)過(guò)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05Java參數(shù)校驗(yàn)詳解之使用@Valid注解和自定義注解進(jìn)行參數(shù)驗(yàn)證
在后端開(kāi)發(fā)中,參數(shù)校驗(yàn)是非常普遍的,下面這篇文章主要給大家介紹了關(guān)于Java參數(shù)校驗(yàn)詳解之使用@Valid注解和自定義注解進(jìn)行參數(shù)驗(yàn)證的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06詳解關(guān)于SpringBoot的外部化配置使用記錄
這篇文章主要介紹了詳解關(guān)于SpringBoot的外部化配置使用記錄,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05Mybatis3中方法返回生成的主鍵:XML,@SelectKey,@Options詳解
這篇文章主要介紹了Mybatis3中方法返回生成的主鍵:XML,@SelectKey,@Options,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01java編程創(chuàng)建型設(shè)計(jì)模式工廠方法模式示例詳解
這篇文章主要為大家介紹了java編程創(chuàng)建型設(shè)計(jì)模式之工廠方法模式的創(chuàng)建及案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02