`
danan.2009
  • 浏览: 12919 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

Spring mock JNDI for datasource

 
阅读更多

项目中的测试用例,有时需要编写依赖与服务环境的测试场景,比如说有一个数据源使用JNDI的方式注册,在测试的junit中可以mockJNDI环境。

比如现在有以下配置:

<?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"
	xmlns:jee="http://www.springframework.org/schema/jee"
	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
                           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
                           ">


<jee:jndi-lookup id="db" jndi-name="jdbc/db" ></jee:jndi-lookup>

</beans:beans>

 我们的测试类Dao依赖JNDI数据源,测试用例如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations=("classpath:smot/applicationConfig.xml"))
public class WebServiceTest {

	@BeforeClass
	public static void setUpBeforClass() throws IllegalStateException, NamingException{
		ClassPathXmlApplicationContext app =new ClassPathXmlApplicationContext("classpath:smot/applicationInit.xml");
		DataSource ds =(DataSource) app.getBean("dataSource");
		SimpleNamingContextBuilder builder =new SimpleNamingContextBuilder();
		builder.bind("java:comp/env/jdbc/db", ds);
		builder.activate();
	}
	
	@Resource
	private GroupManager gm=null;
	
	@Autowired
	private DataSource db=null;
	
	@Test
	public void getUserListTest(){
		Assert.assertNotNull(db);
		Assert.assertNotNull("User Test ", gm.getUserList(new User("1", "std")));
	}

}

 初始化配置如下:

<?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:xe" />
		<beans:property name="username" value="system" />
		<beans:property name="password" value="danan" />
	</beans:bean>

</beans:beans>

 

OK,that 's all.

分享到:
评论

相关推荐

    Spring整合Hibernate案例

    - **配置DataSource**:定义数据源,可以使用Spring的`DataSource`实现,如`BasicDataSource`或JNDI数据源。 - **配置SessionFactoryBean**:将DataSource与Hibernate配置文件链接起来,并设置其他属性如缓存策略...

    Simple-JNDI:通过JNDI查找访问属性文件。 从JNDI获取数据源

    因此,Spring建议替换其已弃用的JNDI Mock实现(请参阅 )。 Simple-JNDI旨在解决的第二个问题是从应用程序中的任何位置轻松访问应用程序配置。 如果您唯一的目的是测试或使用依赖于Tomcat之外的Tomcat JNDI环境的...

    spring-boot-reference.pdf

    Auto-configured Spring REST Docs Tests with Mock MVC Auto-configured Spring REST Docs Tests with REST Assured 43.3.20. User Configuration and Slicing 43.3.21. Using Spock to Test Spring Boot ...

    spring就是这么简单的源码,运行可能需要配置

    9. **单元测试与集成测试**:Spring提供了丰富的测试支持,如`@SpringBootTest`、`@Autowired`和`@MockBean`等注解,用于编写单元测试和集成测试。 10. **问题排查**:在配置过程中遇到问题时,可以查阅官方文档、...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

Global site tag (gtag.js) - Google Analytics