Complete unit testing in spring Web

Source: Internet
Author: User

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

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.