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();
        }

  • 相关阅读:
    《C和指针》读书笔记——第一章 快速上手
    《C和指针》读书笔记——第三章 数据
    Linux下简易蜂鸣器驱动代码及测试实例
    Linux下GPIO驱动(五) misc_register();
    Linux下实现流水灯等功能的LED驱动代码及测试实例
    Linux下GPIO驱动(四) gpio_request();gpio_free();
    支持阻塞操作和轮询操作的globalfifo设备驱动代码分析以及测试代码
    基于等待队列及poll机制的按键驱动代码分析和测试代码
    Linux下GPIO驱动(一) 一个简单的LED驱动
    虚拟内存设备驱动memdev及实例代码
  • 原文地址:https://www.cnblogs.com/widget90/p/9038018.html
Copyright © 2011-2022 走看看