16

I'd like Spring Boot to use a MySQL test database that exists next to the application database for integration tests. At the moment, it's using a H2 database automatically because I added the H2 dependency in Gradle.

This test for example now runs using the H2 database, where I'd rather have it used a physical secondary database.

import org.junit.Test;
import org.junit.runner.RunWith;
import org.observer.media.model.MediaGroup;
import org.observer.media.repository.MediaGroupRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

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

@RunWith(SpringRunner.class)
@SpringBootTest
public class MediaGroupServiceTest {

    @Autowired
    private MediaGroupService mediaGroupService;
    @Autowired
    private MediaGroupRepository mediaGroupRepository;

    @PersistenceContext
    private EntityManager entityManager;

    private MediaGroup mediaGroup = new MediaGroup("name", "ceo", "owner");

    @Test
    public void save() {
        MediaGroup entity = mediaGroupService.saveNew(mediaGroup);

        assertThat(mediaGroupRepository.findByName(mediaGroup.getName())).isEqualTo(entity);
    }
}
progonkpa
  • 3,590
  • 9
  • 31
  • 50
  • 3
    Probably duplicate of https://stackoverflow.com/questions/28007686/how-can-i-provide-different-database-configurations-with-spring-boot – Akash Aug 12 '17 at 18:48

3 Answers3

16

I had application.properties in /src/main/java/resources with a data source configuration for the main application.

I added application-test.properties to /src/test/java/resources with a data source configuration to a database for testing. Additionally, I added @ActiveProfiles("test") to the test that should use that database.

Note that Spring configures itself using the word test in application-test.properties and in the annotation. As such, Spring overrides the configuration of application.properties.

application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/database
spring.datasource.username=user
spring.datasource.password=secret
spring.datasource.driverClassName=com.mysql.jdbc.Driver

application-test.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/database_test
spring.datasource.username=user
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
progonkpa
  • 3,590
  • 9
  • 31
  • 50
4

Though the question already has an answer.

We can also use @DataJpaTest if you want to test JPA applications. By default it will configure an in-memory embedded database, scan for @Entity classes and configure Spring Data JPA repositories. Regular @Component beans will not be loaded into the ApplicationContext.

It is one of the testing improvements made in spring boot application.

Read Docs : https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html

Barath
  • 5,093
  • 1
  • 17
  • 42
4

The in-memory database is used by default on tests. You can disable that behavior and get it to use the application-configured database adding the annotation @AutoConfigureTestDatabase(replace = Replace.NONE) to your tests (see Auto-configured Data JPA Tests).

You can then either add an application.properties or equivalent to src/test/resources or a separate application file such as application-test.properties and make the tests use it by annotating them with @ActiveProfiles("test").

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681