Spring + Junit4 for Interface Test instance code, springjunit4
This article focuses on an example of Spring + Junit4 interface testing. The specific implementation code is as follows.
1. Configure pom. xml
<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. Configure bean
TestInterface can be manually configured or automatically scanned
Manual Configuration
Spring configuration file Configuration:
<bean id="testInterface" class="com.xxx.TestInterfaceImpl"> </bean>
Automatic Scanning
Interface implementation class configuration
@Component public class TestInterfaceImpl implements TestInterface {
Spring configuration file configuration
<context:annotation-config/> <context:component-scan base-package="com.xxx.servlet"> </context:component-scan>
3. Write the Interface Test code
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); } Summary
The above is all the content about the code of the Spring + Junit4 Interface Test instance. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!