Junit+spring创建JNDI运行测试用例
2016-09-09 15:27
471 查看
项目中数据源采用JNDI的方式,因为JNDI由容器提供,
因此在跑Junit用例时,必须要先创建一个JNDI才行。
其实用spring创建jndi十分的简单,首先编写一个测试用的创建数据源信息的配置文件:
然后直接在代码中加载创建就可以了,见代码:
这样就成功创建了一个名为OracleDS的jndi,在跑Junit时就可以获取到了。
顺便附件上spring中获取jndi的配置:
因此在跑Junit用例时,必须要先创建一个JNDI才行。
其实用spring创建jndi十分的简单,首先编写一个测试用的创建数据源信息的配置文件:
<?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="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <beans:property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <beans:property name="url" value="jdbc:oracle:thin:@localhost:1521/test" /> <beans:property name="username" value="developer" /> <beans:property name="password" value="developer" /> </beans:bean> </beans:beans>
然后直接在代码中加载创建就可以了,见代码:
@BeforeClass public static void beforeClass() throws Exception{ ClassPathXmlApplicationContext app =new ClassPathXmlApplicationContext("classpath:InitJndi.xml"); DataSource ds =(DataSource) app.getBean("dataSource"); SimpleNamingContextBuilder builder =new SimpleNamingContextBuilder(); builder.bind("java:OracleDS", ds); builder.activate(); }
这样就成功创建了一个名为OracleDS的jndi,在跑Junit时就可以获取到了。
顺便附件上spring中获取jndi的配置:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"> <value>java:OracleDS</value> </property> </bean>
相关文章推荐
- Junit+spring创建JNDI运行测试用例
- linux下junit测试用例编译与运行配置
- Spring整合JUnit实现测试用例
- Junit多用例测试1-运行多个测试类
- spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例
- Spring 使用Junit的MockMvc 写测试用例
- 基于spring和mybatis项目的JUnit测试用例的实现
- junit4.9测试用例 spring测试用例 Assert 注解
- springboot + mybatis +junit 测试用例编写
- spring、junit测试用例配置与写法
- Junit 实例精讲基础教程(五) JUnit套件测试,一次性运行多个测试用例(测试类的测试方法)
- 使用Ant运行JUnit测试用例时,报java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing的一种解决方案
- junit测试用例加载spring配置文件
- junit 测试spring-test 代码!在项目中运行测试通过,留着以后直接使用
- Subsection类创建测试用例
- spring junit 测试 (二)
- JUnit in java 真正的测试用例实战
- 基于Spring测试的jndi配置问题
- 为什么执行测试用例时报需要 org.junit 4???
- SQL存储过程测试——创建测试用例以及测试结果存储