如何在 Spring boot 1.4.0 中为 @DataJpaTest 排除/禁用特定的自动配置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39253152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to exclude/disable a specific auto-configuration in Spring boot 1.4.0 for @DataJpaTest?
提问by LAC
I am using the @DataJpaTest from Spring for my test which will then use H2 as in memory database as described here. I'm also using Flyway for production. However once the test starts FLyway kicks in and reads the SQL file. How can I exclude the FlywayAutoConfiguration and keep the rest as described here in spring documentationin order to let Hibernate create the tables in H2 for me?
我正在使用 Spring 的 @DataJpaTest 进行我的测试,然后将使用 H2 作为内存数据库中的描述here。我也在使用 Flyway 进行生产。然而,一旦测试开始,FLyway 就会启动并读取 SQL 文件。我如何排除 FlywayAutoConfiguration 并按照 spring 文档中的说明保留其余部分,以便让 Hibernate 在 H2 中为我创建表?
@RunWith(SpringRunner.class)
@DataJpaTest
public class MyRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private MyRepository triggerRepository;
}
回答by Livia Moroianu
Have you tried the @OverrideAutoConfiguration
annotation?
It says it "can be used to override @EnableAutoConfiguration
".
I'm assuming that from there you can somehow exclude FlywayAutoConfiguration
like so:
您是否尝试过@OverrideAutoConfiguration
注释?它说它“可以用来覆盖@EnableAutoConfiguration
”。我假设从那里你可以以某种方式排除FlywayAutoConfiguration
这样的:
@EnableAutoConfiguration(exclude=FlywayAutoConfiguration.class)
回答by Trevor Gowing
Adding the dependency on an in-memory database to my build.gradle
e.g. testRuntime "com.h2database:h2:1.4.194"
将内存数据库的依赖添加到我的 build.gradle
例如testRuntime "com.h2database:h2:1.4.194"
And adding flyway.enabled=false
to application.properties in src/test/resources worked for me.
添加flyway.enabled=false
到 src/test/resources 中的 application.properties 对我有用。
回答by padisah
In my particular case, i needed to disable the FlywayDB on in-memory integration tests. These are using a set of spring annotations for auto-configuring a limited applicationContext.
在我的特殊情况下,我需要在内存集成测试中禁用 FlywayDB。这些使用一组 spring 注释来自动配置有限的 applicationContext。
@ImportAutoConfiguration(value = TestConfig.class, exclude = FlywayAutoConfiguration.class)
@ImportAutoConfiguration(value = TestConfig.class, exclude = FlywayAutoConfiguration.class)
the exclude could effectively further limit the set of beans initiated for this test
exclude 可以有效地进一步限制为此测试启动的 bean 集
回答by Adam
I am converting an old JDBC app into a spring-data-jpa app and I'm working on the first tests now. I kept seeing a security module instantiation error from spring-boot as it tried to bootstrap the security setup, even though @DataJpaTest
should theoretically be excluding it.
我正在将旧的 JDBC 应用程序转换为 spring-data-jpa 应用程序,现在我正在进行第一个测试。我一直从 spring-boot 看到安全模块实例化错误,因为它试图引导安全设置,即使@DataJpaTest
理论上应该排除它。
My problem with the security module probably stems from the pre-existing implementation which I inherited using PropertySourcesPlaceholderConfigurer
(via my PropertySpringConfig
import below)
我的安全模块问题可能源于我继承使用的预先存在的实现PropertySourcesPlaceholderConfigurer
(通过PropertySpringConfig
下面的导入)
Following the docs here:
遵循此处的文档:
http://docs.spring.io/spring-boot/docs/1.4.x/reference/htmlsingle/#test-auto-configuration
http://docs.spring.io/spring-boot/docs/1.4.x/reference/htmlsingle/#test-auto-configuration
and your comments on @LiviaMorunianu's answer, I managed to work my way past every spring-boot exception and get JUnit to run with an auto-configured embedded DB.
以及您对@LiviaMorunianu 的回答的评论,我设法克服了每个 spring-boot 异常,并使 JUnit 与自动配置的嵌入式数据库一起运行。
My main/production spring-boot bootstrap class bootstraps everything including the stuff I want to exclude from my tests. So instead of using @DataJpaTest
, I copied much of what it is doing, using @Import
to bring in the centralized configurations that every test / live setup will use.
我的主要/生产 spring-boot 引导程序类引导所有内容,包括我想从测试中排除的内容。因此@DataJpaTest
,我没有使用,而是复制了它正在做的大部分工作,@Import
用于引入每个测试/实时设置将使用的集中配置。
I also had issues because of the package structure I use, since initially I was running the test which was based in com.mycompany.repositories
and it didn't find the entities in com.mycompany.entities
.
我也有过,因为封装结构我使用的问题,因为最初我跑这是基于在测试中com.mycompany.repositories
,并没有发现在实体com.mycompany.entities
。
Below are the relevant classes.
下面是相关的类。
JUnit Test
JUnit 测试
@RunWith(SpringRunner.class)
@Transactional
@Import({TestConfiguration.class, LiveConfiguration.class})
public class ForecastRepositoryTests {
@Autowired
ForecastRepository repository;
Forecast forecast;
@Before
public void setUp() {
forecast = createDummyForecast(TEST_NAME, 12345L);
}
@Test
public void testFindSavedForecastById() {
forecast = repository.save(forecast);
assertThat(repository.findOne(forecast.getId()), is(forecast));
}
Live Configuration
实时配置
@Configuration
@EnableJpaRepositories(basePackages = {"com.mycompany.repository"})
@EntityScan(basePackages = {"com.mycompany.entity"})
@Import({PropertySpringConfig.class})
public class LiveConfiguration {}
Test Configuration
测试配置
@OverrideAutoConfiguration(enabled = false)
@ImportAutoConfiguration(value = {
CacheAutoConfiguration.class,
JpaRepositoriesAutoConfiguration.class,
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class,
TransactionAutoConfiguration.class,
TestDatabaseAutoConfiguration.class,
TestEntityManagerAutoConfiguration.class })
public class TestConfiguration {
// lots of bean definitions...
}
PropertySpringConfig
属性弹簧配置
@Configuration
public class PropertySpringConfig {
@Bean
static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()
throws IOException {
return new CorePropertySourcesPlaceholderConfigurer(
System.getProperties());
}
}
回答by bitmountain
You can just disable it in your test yaml file:
您可以在测试 yaml 文件中禁用它:
flyway.enabled: false
flyway.enabled: false
回答by Kristoffer
I resolved the same issue by excluding the autoconfiguration from my application definition, i.e.
我通过从我的应用程序定义中排除自动配置解决了同样的问题,即
@SpringBootApplication(exclude = {FlywayAutoConfiguration.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
回答by virgium03
I had the same problem with my DbUnit tests defined in Spock test classes. In my case I was able to disable the Flyway migration and managed to initialize the H2 test database tables like this:
我在 Spock 测试类中定义的 DbUnit 测试遇到了同样的问题。就我而言,我能够禁用 Flyway 迁移并设法初始化 H2 测试数据库表,如下所示:
@SpringBootTest(classes = MyApplication.class, webEnvironment = SpringBootTest.WebEnvironment.NONE,
properties = ["flyway.enabled=false", "spring.datasource.schema=db/migration/h2/V1__init.sql"])
I added this annotation to my Spock test specification class. Also, I was only able to make it work if I also added the context configuration annotation:
我将此注释添加到我的 Spock 测试规范类中。此外,如果我还添加了上下文配置注释,我只能使其工作:
@ContextConfiguration(classes = MyApplication.class)
回答by Seyed Ketabchi
you can also sue the following annotation:
您还可以起诉以下注释:
@RunWith(SpringRunner.class)
@DataJpaTest(excludeAutoConfiguration = {MySqlConfiguration.class, ...})
public class TheClassYouAreUnitTesting {
}