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);
}
}
Multiple Databases in Spring Boot The interesting part is annotating the data source bean creation method with @ConfigurationProperties. We just need to specify the corresponding config prefix. Inside this method, we're using a DataSourceBuilder, and Spring Boot will automatically take care of the rest.
Spring Boot gives you defaults on all things. For example, the default database is H2 . Consequently, when you want to use any other database, you must define the connection attributes in the application. properties file.
Using a Standard Properties File in Spring Bootproperties. It resides in the src/main/resources folder. If we want to use different properties for tests, we can override the properties file in the main folder by placing another file with the same name in src/test/resources. The application.
The easiest way to test the database connection from Spring boot is to start the application and by checking to debug logs.
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 "overwrites" 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With