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