samedi 12 août 2017

How to tell Spring Boot to use another DB for test?

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);
    }
}

Aucun commentaire:

Enregistrer un commentaire