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;
}
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:
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:
Have you tried the
@OverrideAutoConfiguration
annotation? It says it "can be used to override@EnableAutoConfiguration
". I'm assuming that from there you can somehow excludeFlywayAutoConfiguration
like so:you can also sue the following annotation:
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 myPropertySpringConfig
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 incom.mycompany.entities
.Below are the relevant classes.
JUnit Test
Live Configuration
Test Configuration
PropertySpringConfig
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
You can just disable it in your test yaml file:
flyway.enabled: false