本文研究的主要是Spring+Junit4進行接口測試的一個相關(guān)實例,具體實現(xiàn)代碼如下。
1.配置pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
< dependency > < groupId >org.springframework</ groupId > < artifactId >spring-context</ artifactId > < version >4.3.2.RELEASE</ version > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-test</ artifactId > < version >4.3.2.RELEASE</ version > </ dependency > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.12</ version > </ dependency > |
2.配置bean
testInterface可以進行手動配置或自動掃描
手動配置
spring配置文件配置:
1
2
|
< bean id = "testInterface" class = "com.xxx.TestInterfaceImpl" > </ bean > |
自動掃描
接口實現(xiàn)類中配置
1
2
|
@Component public class TestInterfaceImpl implements TestInterface { |
spring配置文件配置
1
2
3
4
5
|
< context:annotation-config /> < context:component-scan base-package = "com.xxx.servlet" > </ context:component-scan > |
3.編寫接口測試代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith (SpringJUnit4ClassRunner. class ) @ContextConfiguration (locations = "classpath:applicationContext.xml" ) public class Test { @Resource TestInterface testInterface; @Test public void test1(){ testInterface.test1( 1 , 2 ); } |
總結(jié)
以上就是本文關(guān)于Spring+Junit4進行接口測試實例代碼的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/fengqingting2/article/details/52650126