使用Spring Boot测试时仅从测试/资源加载数据 [英] Load data only from test/resources while testing with Spring Boot

查看:67
本文介绍了使用Spring Boot测试时仅从测试/资源加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与Hibernate集成的Spring Boot应用程序,用于数据库持久性.

I have an Spring Boot application integrated with Hibernate for the database persistence.

我有两个不同的data.sql文件:

I have two different data.sql files:

  • src/main/resources中的一个用于数据库初始化
  • 用于测试目的的src/test/resources中的一个

在测试时,它们都在任何测试类之前加载.但是,我只想加载一个test/resources,只保留main/resources,仅用于应用程序初始化.

While testing, both of them are loaded before any test class. However, I only want to load the test/resources one, leaving the main/resources only and solely for app initialization.

我该怎么办?

谢谢.

推荐答案

维护一个单独的属性文件以进行测试,因为它将帮助您隔离测试和开发之间的内容.

Maintain a separate properties file for test as it will help you in segregating the things between test and development.

application.properties for dev
application-local.properties for local env
application-test.properties for test 

根据需要拥有配置文件,并具有与配置文件有关的属性.

Have profiles according to your need and have properties with respect to profiles.

另一种替代方法是根据您的需要覆盖属性.

Another alternative is to override the properties based on your need.

这篇关于使用Spring Boot测试时仅从测试/资源加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆