项目中的测试用例,有时需要编写依赖与服务环境的测试场景,比如说有一个数据源使用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.