Junit BeforeClass 怎么获取数据库连接?

想在BeforeClass里面获取数据库连接,在跑测试之前把当前数据库中的数据线备份掉。
不太想要在里面写死,想调用root-context.xml里面配置的数据库连接

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring/root-context.xml"})
public class Demo extends JunitBase {

    @Autowired
    private DemoService testClass;
    @BeforeClass
    public static void beforeAll() throws Exception {
    // 在这里怎么获取root-context.xml里面配置的数据库连接
    }

root-context.xml

    <context:property-placeholder location="classpath:mysql.properties" />

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>
阅读 3.5k
1 个回答

为什么要备份呢?难道你在生产数据库跑测试代码?虽然测试会回滚,但是千万不要在生产库跑测试。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题