Consolidate JUnit for testing:
JUnit has natural support for spring, and just add two annotation to start the spring context, the sample code is as follows:
@ContextConfiguration ("/meta-inf/spring/integration/inbound-gateway-config.xml")
@RunWith ( Springjunit4classrunner.class) Public
class Inboundgatewaytests {
@Autowired
private Simplewebserviceinboundgateway Gateway;
}
Integrate testng for testing:
Unlike JUnit, testng does not provide @runwith annotations. TestNG test cases need to inherit org.springframework.test.context.testng.AbstractTestNGSpringContextTests or Org.springframework.test.context.testng.AbstractTestNGSpringContextTests to start Springcontext, the sample code is as follows:
@ContextConfiguration (locations={"/web-inf/accountcontext.xml"}) public
class Testdao extends abstracttestngspringcontexttests {
@Autowired
jdbctemplate jdbctemplate;
@Test public
void Init () throws Servletexception {
try {
jdbctemplate.execute ("CREATE Table Contacts (name varchar (), email varchar, phone varchar) ");
List List = Jdbctemplate.queryforlist ("SELECT * from Contacts");
} catch (Exception e) {
e.printstacktrace (System.out);}}
}
The settings for the @contextconfiguration parameter are explained below:
1. Do not specify a context file, @ContextConfiguration
Spring's contextloader will determine if the context needs to be loaded, and if necessary, the default loader (Genericxmlcontextloader) will generate a spring context configuration file name based on the use case class name, for example, If the test case is com.example.MyTest, Genericxmlcontextloader loads the context from "Classpath:/com/example/mytest-context.xml".
2. Specify multiple context files, such as @contextconfiguration ("/meta-inf/spring/integration/inbound-gateway-config.xml")
3. Specify multiple context files, such as @contextconfiguration (locations={"/applicationcontext.xml", "/applicationcontext-test.xml"})
Contextloader will load the above files from the root directory of the classpath.
4. The impact of inheritance on context file loading, the use of inheritlocations.
@RunWith (Springjunit4classrunner.class)
@ContextConfiguration (locations={"/base-context.xml"})
Public Class Basetest {
//class body ...
}
@ContextConfiguration (locations={"/extended-context.xml"}) public
class Extendedtest extends Basetest {
// Class body ...
}
In the above code extendedtest will load "/base-context.xml" and "/extended-context.xml" two files, because the default value of Inheritlocations is true.
From the above explanation, we can see that the spring context file is located based on Classpath. Under Eclipse, the classpath settings are often different from the build environment, how to set the context file in the build environment to @contextconfiguration. The steps are as follows: Click Run->run configurations ... Select the Run configuration item for the test case you want to run, such as incontainertests, then click User Entries, then click Advanced, select Add External Folder in the pop-up window, and click OK to select any directory. You can configure the parameters in the @contextconfiguration based on the location of the context file relative to the directory.