Reputation: 425
I have a simple Spring boot application. I have it connected to a Postgres database. The URL and login information is in the application.properties file.
This meens that when I am starting the application locally for testing, I need to also have the database up and running. Or else it will fail on startup.
Is there a way to either:
Upvotes: 0
Views: 1117
Reputation: 197
The below dependency to be added to the pom.xml.
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.4.0</version>
<scope>runtime</scope>
</dependency>
Next, in the application.properties file set the test as active profile.
#Profile configuration
spring.profiles.active=test
Next, write the application-test.properties as below:
#Database configuration
spring.datasource.url=jdbc:hsqldb:mem:scratchdb/table_name
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.show-sql = true
application.yml
spring:
profiles:
active: test
server:
servlet.context-path: /
port: 4343
---
spring:
profiles: dev
datasource:
url: jdbc:mysql://localhost:3306/db_name
datasource:
username: root
password: root
jpa:
show-sql: true
---
spring:
profiles: test
datasource:
url: jdbc:hsqldb:mem:scratchdb/db_name
username: sa
password:
jpa:
show-sql: true
Upvotes: 1
Reputation: 7624
Well, your use case is very common and generally using In-memory is preferred when running locally.
Things required to activate it
1.Add below dependency in pom.xml
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>runtime</scope>
</dependency>
you can get other H2 versions from here
2.Can have a local property file application-local.properties
will following content
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
3.Activate local profile while running
mvn spring-boot:run -Dspring.profiles.active=local
OR
You can specify spring.profiles.active
property in application.properties
spring.profiles.active=local
Upvotes: 1
Reputation: 4623
I'd go with your option 1: create a test properties config test/resource/application-test.properties
and annotate the test class with @ActiveProfiles("test")
edit If you want to just run the app w/ a different datasource, Spring Profiles is the way to go.
You declare your db config bean in a @Configuration
class, with the @Profile
annotation:
@Configuration
@Profile("mem")
public class DataSourceConfig {
@Bean
public DataSource getDataSource() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("org.h2.Driver");
dataSourceBuilder.url("jdbc:h2:mem:test");
dataSourceBuilder.username("SA");
dataSourceBuilder.password("");
return dataSourceBuilder.build();
}
}
Alternatively, you can just create a profiled properties file named application-mem.properties
with your db configuration there.
Finally, you just run your app with a profile like so:
java -jar app.jar --spring.profiles.active=mem
Upvotes: 1