在测试类上添加@RunWith注解指定使用springJunit的测试运行器,@ContextConfiguration注解指定测试用的spring配置文件的位置。接着我们就可以注入我们需要测试的bean进行测试,Junit在运行测试之前会先解析spring的配置文件,初始化spring中配置的bean。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:/conf/root-context.xml"})
public class test {
@Autowired
ComboPooledDataSource comboPooledDataSource;
//测试数据源配置
@Test
public void demo1(){
System.out.println(comboPooledDataSource.getDriverClass());
System.out.println(comboPooledDataSource.getJdbcUrl());
System.out.println(comboPooledDataSource.getUser());
System.out.println(comboPooledDataSource.getPassword());
}
}