For the total unit test in Springweb, before looking for some information, groping for a long time, recording the final use of their own methods
1. Create a test class and create a test resource folder src/test/resources/web_info/conf
Put the spring configuration for the project here,
In addition to this directory, create a file Initjndi.xml, which puts the data source information used by the project into the following:
<?xml version= "1.0" encoding= "UTF-8"?>
<beans:beans xmlns:beans= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx"
xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.0.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
<beans:bean id= "DB1" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<beans:property name= "Driverclassname" value= "Oracle.jdbc.OracleDriver"/>
<beans:property name= "url" value= "Jdbc:oracle:thin:@1.1.1.1:1521:orcl"/>
<beans:property name= "username" value= "db1"/>
<beans:property name= "Password" value= "DB1"/>
</beans:bean>
<beans:bean id= "DB2" class= "Org.springframework.jdbc.datasource.DriverManagerDataSource" >
<beans:property name= "Driverclassname" value= "Oracle.jdbc.OracleDriver"/>
<beans:property name= "url" value= "Jdbc:oracle:thin:@//2.2.2.2:1521/orcl"/>
<beans:property name= "username" value= "DB2"/>
<beans:property name= "Password" value= "DB2"/>
</beans:bean>
</beans:beans>
2. Add a reference to the spring file in the test class
@RunWith (Springjunit4classrunner.class)
@Configuration
@ImportResource ({"Classpath:./web-inf/conf/spring.application-context.xml"})
@ContextConfiguration (Classes =parserserviceimpltest.class)
public class Parserserviceimpltest {
... ...
}
3, add the Beforeclass method of the class, and complete a reference to the data source, as follows:
@BeforeClass
public static void Beforeclass () throws Exception {
Classpathxmlapplicationcontext app = new Classpathxmlapplicationcontext (
"Classpath:./web-inf/conf/initjndi.xml"); Referencing a data source configuration file
DataSource db1= (DataSource) App.getbean ("DB1");
DataSource db2= (DataSource) App.getbean ("DB2");
Simplenamingcontextbuilder builder = new Simplenamingcontextbuilder ();
Builder.bind ("DB1", DB1);
Builder.bind ("DB2", DB2);
Builder.activate ();
}
Complete the testing of the corresponding service in the test method
@Autowired
Iparserservice Parserservice;
@Test
public void Handlemessagetest () {
Parserservice.handlemessage ();
}
Complete unit testing in spring Web