1:和junit一起使用的時(shí)候因?yàn)闆]有讀取配置文件,所以老是報(bào)創(chuàng)建Bean失敗,上網(wǎng)查了查,原來(lái)是先要讀取spring的核心配置文件,這樣機(jī)也能夠啟動(dòng)IOC容器了,
可以先創(chuàng)建一個(gè)父類,在父類里面讀取配置文件創(chuàng)建IOC容器,然后讓子類繼承他就可以了
BaseTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.carry.ssm.test; import javax.annotation.Resource; import javax.security.auth.PrivateCredentialPermission; import org.junit.Test; import org.junit.runner.RunWith; import com.carry.ssm.Services.TestServer; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * 配置spring和junit整合,junit啟動(dòng)時(shí)加載springIOC容器 */ @RunWith (SpringJUnit4ClassRunner. class ) //告訴junit spring配置文件 @ContextConfiguration ( "classpath:applictionContext.xml" ) //我是放在classpath下的,可以根據(jù)自己的路徑改 public class baseTest { } |
2:寫測(cè)試類
TestUnit.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package com.carry.ssm.test; import javax.annotation.Resource; import org.junit.Test; import com.carry.ssm.Model.User; import com.carry.ssm.Services.TestServer; import com.carry.ssm.Services.UserService; public class TestUnit extends baseTest{ @Resource private TestServer bean; @Resource private UserService userService; //注意,這里要用接口,因?yàn)橛玫搅藄pring的AOP /*@Test*/ /* public void inteceptorTest(){ bean.ttst(); } */ @Test public void getUser(){ User user= new User(); user.setUSER_NAME( "carry" ); userService.login(user); } } |
打印如下
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/CarryBest/article/details/71123493