How to exclude/disable a specific auto-configuration in Spring boot 1.4.0 for @DataJpaTest?
Asked Answered
A

8

17

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;
}
Aseptic answered 31/8, 2016 at 15:5 Comment(1)
Did @Leis help? I'm tackling a similar issue.Qoph
C
7

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)
Cerallua answered 31/8, 2016 at 15:13 Comment(5)
The annotation says it can be used in combination with @ ImportAutoConfiguration. The exclusion using @ EnableAutoConfiguration doesn't work. I tried to add every auto-configuration class except for the Flyway just to see whether it works that way or not. Some parts are working (e.g. Flyway doesn't kick in anymore) some don't. E.g the Transaction is not working and I have to use @ Transactional. Also the Tables are not created by Hibernate automatically. There are missing parts and actually I prefer to get the exclusion running. I'm using the following config now.Aseptic
@ RunWith(SpringRunner.class) @ SpringBootTest @ OverrideAutoConfiguration(enabled = false) @ ImportAutoConfiguration(value = { CacheAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class, DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class, TestDatabaseAutoConfiguration.class, TestEntityManagerAutoConfiguration.class }) @ TransactionalAseptic
I've raised github.com/spring-projects/spring-boot/issues/6809 to see what we can do to improve things here.Woodford
@PhilWebb that's great ;) but do you have a workaround for now?Aseptic
Not yet I'm afraid.Woodford
D
4

Adding the dependency on an in-memory database to my build.gradle
e.g. testRuntime "com.h2database:h2:1.4.194"

And adding flyway.enabled=false to application.properties in src/test/resources worked for me.

Designer answered 2/4, 2017 at 19:0 Comment(1)
Thank you! That worked for me. The key here is to instruct flyway to be disabled (as you answered)! e.g. flyway.enabled=falseTheresita
Q
2

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());
    }
}
Qoph answered 9/11, 2016 at 17:6 Comment(1)
I had a nice @DataJpaTest working (spring 5.04) and testing only my simple repository. Then I added a dependency to SchemaRegistryClient and my totally unrelated JPA test stopped working. It was suddenly complaining of a missing CacheManager bean ... I ended up stacking a similar pile of annotation and configuration class to get it back working :-/ Hope the @DataJpaTest annotation will be improvedConservative
M
2

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

Move answered 17/7, 2018 at 15:25 Comment(2)
The annotation has changed exactly because of this question. At the time of writing this the option of exclusion didn't exist, therefore this issue was created on Spring boot side: github.com/spring-projects/spring-boot/issues/6809Aseptic
I am happy to hear this. I just wanted to help out followers, and this question has no accepted answer yet.Move
L
1

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)
Leis answered 15/9, 2016 at 14:56 Comment(0)
I
1

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);
    }
}
Intolerant answered 7/11, 2017 at 9:42 Comment(0)
A
1

you can also sue the following annotation:

@RunWith(SpringRunner.class)
@DataJpaTest(excludeAutoConfiguration = {MySqlConfiguration.class, ...})
public class TheClassYouAreUnitTesting {
}
Anaerobic answered 30/9, 2018 at 0:15 Comment(0)
M
1

You can just disable it in your test yaml file:

flyway.enabled: false

Marduk answered 17/4, 2019 at 9:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.