How to exclude/disable a specific auto-configurati

2019-04-19 05:08发布

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 documentation in order to let Hibernate create the tables in H2 for me?

@RunWith(SpringRunner.class)
@DataJpaTest
public class MyRepositoryTest {

    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private MyRepository triggerRepository;
}

8条回答
你好瞎i
2楼-- · 2019-04-19 05:44

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:

@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:

@ContextConfiguration(classes = MyApplication.class)
查看更多
Root(大扎)
3楼-- · 2019-04-19 05:46

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:

@EnableAutoConfiguration(exclude=FlywayAutoConfiguration.class)
查看更多
We Are One
4楼-- · 2019-04-19 05:46

you can also sue the following annotation:

@RunWith(SpringRunner.class)
@DataJpaTest(excludeAutoConfiguration = {MySqlConfiguration.class, ...})
public class TheClassYouAreUnitTesting {
}
查看更多
淡お忘
5楼-- · 2019-04-19 05:50

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.

My problem with the security module probably stems from the pre-existing implementation which I inherited using PropertySourcesPlaceholderConfigurer (via my PropertySpringConfig import below)

Following the docs here:

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.

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.

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.

Below are the relevant classes.

JUnit Test

@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());
    }
}
查看更多
神经病院院长
6楼-- · 2019-04-19 05:55

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.

@ImportAutoConfiguration(value = TestConfig.class, exclude = FlywayAutoConfiguration.class)

the exclude could effectively further limit the set of beans initiated for this test

查看更多
小情绪 Triste *
7楼-- · 2019-04-19 05:55

You can just disable it in your test yaml file:

flyway.enabled: false

查看更多
登录 后发表回答