Spring + Junit4 for Interface Test instance code, springjunit4

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.