私は、Springテストで、統合テストのためにアプリケーションデータベースの横にあるMySQLテストデータベースを使用したいと考えています。現時点では、H2データベースを自動的に使用しています。これは、GradleにH2依存関係を追加したためです。Spring Bootにテスト用に別のDBを使用するように指示するには?
このテストの例はH2データベースを使用して実行されます。ここでは、物理的なセカンダリデータベースを使用しています。
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);
}
}
おそらくhttps://stackoverflow.com/questions/28007686/how-can-i-provide-different-database-configurations-with-spring-bootの複製 – Akash