Spring: test your MySQL JPA with H2

How to test your repository using H2 when you use MySQL in production.

If in Spring Boot you are using a database you can use for your tests H2 or another similar embedded database.

In our case we are using MySQL / PostgreSQL for production and we want to test our code using @DataJpaTest.

Some relevant information from the documentation:



Include the DB in the classpath

In our case we use H2, we import the library in Maven

``` xml

com.h2database h2 test


You have to tell Spring to use the H2 database during the tests, for this you have to create `/src/test/resources/application.properties` with the connection information for H2

``` bash
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.username=sa
spring.datasource.password=sa

We create the test class with the required dependencies

``` java import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional;

import static org.assertj.core.api.Assertions.*;

@RunWith(SpringRunner.class) @DataJpaTest @Transactional(propagation = Propagation.NOT_SUPPORTED) class FoodControllerTest {

@Autowired FoodRepository carbsRepository;

@Test void createFood() { ... foodRepository.save(carbs); assertThat(foodRepository.count()).isEqualTo(1); }