zoukankan      html  css  js  c++  java
  • spring web中完成单元测试

    对于在springweb总完单元测试,之前找过些资料,摸索了很久,记录下最终自己使用的方法

    1,创建测试类,创建测试资源文件夹 src/test/resources/WEB_INFO/conf

    将工程用的spring配置放在这里,

    另外在此目录下创建文件InitJndi.xml,其中放入工程使用的数据源信息,内容入下:

    <?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,在测试类中增加spring文件的引用

    @RunWith(SpringJUnit4ClassRunner.class)
    @Configuration
    @ImportResource({ "classpath:./web-inf/conf/spring.application-context.xml" })
    @ContextConfiguration(classes =ParserServiceImplTest.class)
    public class ParserServiceImplTest {

      ...  ...

    }

    3,增加类的beforeClass方法,并在其中完成数据源的引用,如下:

        @BeforeClass
        public static void beforeClass() throws Exception {
            ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(
                    "classpath:./web-inf/conf/InitJndi.xml");  // 引用数据源配置文件
            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();
        }

    在测试方法中完成对应Service的测试工作

       @Autowired
        IParserService parserService;

       @Test
        public void handleMessageTest() {
            parserService.handleMessage();
        }

  • 相关阅读:
    Python之迭代器,生成器
    Python函数--装饰器进阶
    Python之函数的本质、闭包、装饰器
    Python之函数--命名空间、作用域、global、nonlocal、函数的嵌套和作用域链
    Python函数的定义与调用、返回值、参数
    Python之文件操作
    Python之集合
    基本数据类型补充,深浅copy
    Python基础-元组、列表、字典
    Python常用模块(一)
  • 原文地址:https://www.cnblogs.com/widget90/p/9038018.html
Copyright © 2011-2022 走看看