Spring Boot DataJpaTest unit test reverting to H2 instead of mySql
From @DataJpaTest
documentation:
By default, tests annotated with @DataJpaTest will use an embedded in-memory database (replacing any explicit or usually auto-configured DataSource).
If you go to documentation you can see this annotation aggregates a lot of other annotations.
@Transactional
annotation behaves in different way in test context than in application context:
From spring documentation:
Annotating a test method with @Transactional causes the test to be run within a transaction that is, by default, automatically rolled back after completion of the test.
I believe I provided enough information which answering your question, additionaly you can take a look at following articles:
Configuring Separate Spring DataSource for Tests
Testing with @Configuration Classes and Profiles
By default, the @DataJpaTest
uses in memory H2 database for repo tests. Should you need to use the actual DB, you can consider either to disable the auto Configurations or use @SpringBootTest
where the whole application web mvc is enabled.
To disable auto config:
@RunWith(SpringRunner.class)
@ActiveProfiles("test")
@Transactional
@DataJpaTest
@AutoConfigureTestDatabase(replace=Replace.NONE)
public class IssueRepositoryIntegrationTests
@AutoConfigureTestDatabase
configures the test H2 DB for you. You can specifically mention not to by above or you can exclude this auto configuration as :
@EnableAutoConfiguration(exclude=AutoConfigureTestDatabase.class)
P.S: : I have not tried the above exclusion myself yet.
For more info on that go thru javadoc: https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/jdbc/AutoConfigureTestDatabase.html