本文研究的主要是Spring+Junit4進行接口測試的一個相關實例,具體實現代碼如下。
<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>
testInterface可以進行手動配置或自動掃描
手動配置
spring配置文件配置:
<bean id="testInterface"> </bean>
自動掃描
接口實現類中配置
@Component public class TestInterfaceImpl implements TestInterface {spring配置文件配置
<context:annotation-config/> <context:component-scan base-package="com.xxx.servlet"> </context:component-scan>
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); }以上就是本文關於Spring+Junit4進行接口測試實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!