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++如何在Dialog和View中显示梯度背景颜色
    C++MFC的关键类(View,Application,Frame,Document等等)之间访问方法列表
    C++深入分析MFC文档视图结构(项目实践)
    C++如何修改SDI程序的默认背景颜色
    BAPI使用HR_INFOTYPE_OPERATION函数批量导入HR信息纪录代码样例(0759信息类型)
    C++在单文档的应用程序增加多个视图
    SD定价过程的16个字段的作用说明
    HR上载信息类型的长文本的样例代码
    C++在工具条中加入组合框控件
    C++如何锁定splitter窗口
  • 原文地址:https://www.cnblogs.com/widget90/p/9038018.html
Copyright © 2011-2022 走看看