Spring JUnit Integration Test

Source: Internet
Author: User

Examples are as follows:

 Packagecom.junge.demo.spring;Import Staticorg.junit.Assert.assertEquals;Importjava.util.List;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.test.context.ContextConfiguration;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;ImportCom.junge.demo.spring.service.IPerface;ImportCom.junge.demo.spring.service.ServiceConfig;/*** Unit test for simple App.*/@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Classes=serviceconfig.class) Public classapptest {@AutowiredPrivateList<iperface>perfacelist; @Test Public voidaddtest () {System.out.println ("333"); } @Test Public voidplaytest () {assertequals (2, Perfacelist.size ()); }}

2 annotations are used

[Email protected] This is provided by JUnit, note that the JUnit version is to be compatible with spring, such as spring with the 4.1.13.RELEASE version, Junt can be used in version 4.12, if not compatible, the use case will not run up.

[Email protected] This is a spring-provided annotation that configures the context configuration of spring, if configured using the Javaconfig style, as shown in the previous example, if configured with an XML file, you can use the following configuration:

@ContextConfiguration (locations = {"Classpath:applicationContext.xml"})

Typically, when testing in a project, a base class test class is configured, and other test classes inherit from the base class, so you do not need to add a configuration to each test class.

@RunWith (Springjunit4classrunner.  Class= {"Classpath:applicationContext.xml" })publicabstractclass  baseservicetest {        }

Spring JUnit Integration Test

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.