qq_32266305 2018-08-16 09:59 采纳率: 0%
浏览 1848
已结题

tomcat jndi 数据源 junit单元测试

如何在项目中进行单元测试,数据源在tomcat上 ,底层封装的dbmanager,在tomcat起来时,直接读取配置的数据源 。咋整????????????

  • 写回答

1条回答 默认 最新

  • qq_38094834 2018-08-16 10:19
    关注

    解决方案

    由于项目集成了Spring的自动注入等功能,所以在使用Junit进行单元测试的时候需要保证Spring的配置文件都能被加载,同时需要保证连接数据库的数据源必须被加载,这就需要配置单独的数据源,具体方法如下:

    新建spring_jndi_test.xml
    <?xml version="1.0" encoding="UTF-8"?>
    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">
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">




    /beans:bean
    /beans:beans
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    在Junit测试类中加载配置文件与获取Bean
    public class CommonDAOJdbc_StandardTest {
    private volatile static BeanFactory factory;

    @Test
    public void testGetFirmCanOutBalance() {
        // 获取Bean
        CommonDAO commonDAO = (CommonDAO) factory.getBean("commonDAO");
    
        // 此处可调用CommonDAO类中的方法
    }
    
    @Before
    public void init() {
        System.out.println("加载spring配置开始 ............");
    
        ArrayList<String> list = new ArrayList<String>();
        list.add("spring.xml");            // 将Sprint配置文件加入待加载列表
        list.add("Spring_jndi_test.xml");  // 将测试用的数据源配置文件加入待加载列表
        try {
            factory = new ClassPathXmlApplicationContext(list.toArray(new String[list.size()]));
            // 保证虚拟机退出之前 spring中singtleton对象自定义销毁方法会执行
            ((AbstractApplicationContext) factory).registerShutdownHook();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("加载配置文件时发生错误" + e);
        }
    
        System.out.println("加载spring配置结束.............");
    }
    

    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    至此,便可以进行Junit的单元测试,且数据源也能获取了。

    当然,如果出现“java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver”,那么则需要Build Path -> Add Libraries … 引入ojdbc包即可。

    评论

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统