BAEL-4530: A Guide to @DynamicPropertySource in Spring (#9877)
* A Guide to @DynamicPropertySource in Spring * Moving to a new module * Reverting the Changes in the original module
This commit is contained in:
parent
e0ebf5904f
commit
25b1749308
|
@ -31,7 +31,8 @@
|
|||
<module>rest-assured</module>
|
||||
<module>rest-testing</module>
|
||||
<module>selenium-junit-testng</module>
|
||||
<module>spring-testing</module>
|
||||
<module>spring-testing</module>
|
||||
<module>spring-testing-2</module>
|
||||
<module>test-containers</module>
|
||||
<module>testing-assertions</module>
|
||||
<module>testng</module>
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
.idea/**
|
||||
target/**
|
||||
*.iml
|
|
@ -0,0 +1 @@
|
|||
## Relevant Articles:
|
|
@ -0,0 +1,57 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
||||
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-testing-2</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>spring-testing-2</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Test containers only dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
<version>${testcontainers.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${testcontainers.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- Test containers only dependencies -->
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<spring-boot-version>2.1.9.RELEASE</spring-boot-version>
|
||||
<spring.boot.starter.version>2.1.9.RELEASE</spring.boot.starter.version>
|
||||
<testcontainers.version>1.12.2</testcontainers.version>
|
||||
</properties>
|
||||
</project>
|
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.dynamicproperties;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import static javax.persistence.GenerationType.IDENTITY;
|
||||
|
||||
@Entity
|
||||
@Table(name = "articles")
|
||||
public class Article {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private String content;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.dynamicproperties;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ArticleRepository extends JpaRepository<Article, Long> {
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.dynamicproperties;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DynamicPropertiesApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DynamicPropertiesApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.dynamicproperties;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||
import org.springframework.test.context.DynamicPropertySource;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringBootTest
|
||||
@Testcontainers
|
||||
@ActiveProfiles("pg")
|
||||
public class ArticleLiveTest {
|
||||
|
||||
@Container
|
||||
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:11")
|
||||
.withDatabaseName("prop")
|
||||
.withUsername("postgres")
|
||||
.withPassword("pass")
|
||||
.withExposedPorts(5432);
|
||||
|
||||
@Autowired
|
||||
private ArticleRepository articleRepository;
|
||||
|
||||
@DynamicPropertySource
|
||||
static void registerPgProperties(DynamicPropertyRegistry registry) {
|
||||
registry.add("spring.datasource.url",
|
||||
() -> String.format("jdbc:postgresql://localhost:%d/prop", postgres.getFirstMappedPort()));
|
||||
registry.add("spring.datasource.username", () -> "postgres");
|
||||
registry.add("spring.datasource.password", () -> "pass");
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAnArticle_whenPersisted_thenCanBeFoundInTheDb() {
|
||||
Article article = new Article();
|
||||
article.setTitle("A Guide to @DynamicPropertySource in Spring");
|
||||
article.setContent("Today's applications...");
|
||||
|
||||
articleRepository.save(article);
|
||||
Article persisted = articleRepository.findAll().get(0);
|
||||
assertThat(persisted.getId()).isNotNull();
|
||||
assertThat(persisted.getTitle()).isEqualTo("A Guide to @DynamicPropertySource in Spring");
|
||||
assertThat(persisted.getContent()).isEqualTo("Today's applications...");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.dynamicproperties;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringBootTest
|
||||
@ActiveProfiles("pg")
|
||||
@ExtendWith(PostgreSQLExtension.class)
|
||||
public class ArticleTestFixtureLiveTest {
|
||||
|
||||
@Autowired
|
||||
private ArticleRepository articleRepository;
|
||||
|
||||
@Test
|
||||
void givenAnArticle_whenPersisted_thenShouldBeAbleToReadIt() {
|
||||
Article article = new Article();
|
||||
article.setTitle("A Guide to @DynamicPropertySource in Spring");
|
||||
article.setContent("Today's applications...");
|
||||
|
||||
articleRepository.save(article);
|
||||
Article persisted = articleRepository.findAll().get(0);
|
||||
assertThat(persisted.getId()).isNotNull();
|
||||
assertThat(persisted.getTitle()).isEqualTo("A Guide to @DynamicPropertySource in Spring");
|
||||
assertThat(persisted.getContent()).isEqualTo("Today's applications...");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.dynamicproperties;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@SpringBootTest
|
||||
@Testcontainers
|
||||
@ActiveProfiles("pg")
|
||||
@ContextConfiguration(initializers = ArticleTraditionalLiveTest.EnvInitializer.class)
|
||||
class ArticleTraditionalLiveTest {
|
||||
|
||||
@Container
|
||||
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:11")
|
||||
.withDatabaseName("prop")
|
||||
.withUsername("postgres")
|
||||
.withPassword("pass")
|
||||
.withExposedPorts(5432);
|
||||
|
||||
static class EnvInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
@Override
|
||||
public void initialize(ConfigurableApplicationContext applicationContext) {
|
||||
TestPropertyValues.of(
|
||||
String.format("spring.datasource.url=jdbc:postgresql://localhost:%d/prop", postgres.getFirstMappedPort()),
|
||||
"spring.datasource.username=postgres",
|
||||
"spring.datasource.password=pass"
|
||||
).applyTo(applicationContext);
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private ArticleRepository articleRepository;
|
||||
|
||||
@Test
|
||||
void givenAnArticle_whenPersisted_thenShouldBeAbleToReadIt() {
|
||||
Article article = new Article();
|
||||
article.setTitle("A Guide to @DynamicPropertySource in Spring");
|
||||
article.setContent("Today's applications...");
|
||||
|
||||
articleRepository.save(article);
|
||||
Article persisted = articleRepository.findAll().get(0);
|
||||
assertThat(persisted.getId()).isNotNull();
|
||||
assertThat(persisted.getTitle()).isEqualTo("A Guide to @DynamicPropertySource in Spring");
|
||||
assertThat(persisted.getContent()).isEqualTo("Today's applications...");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.dynamicproperties;
|
||||
|
||||
import org.junit.jupiter.api.extension.AfterAllCallback;
|
||||
import org.junit.jupiter.api.extension.BeforeAllCallback;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
import org.testcontainers.containers.PostgreSQLContainer;
|
||||
|
||||
public class PostgreSQLExtension implements BeforeAllCallback, AfterAllCallback {
|
||||
|
||||
private PostgreSQLContainer<?> postgres;
|
||||
|
||||
@Override
|
||||
public void beforeAll(ExtensionContext context) {
|
||||
postgres = new PostgreSQLContainer<>("postgres:11")
|
||||
.withDatabaseName("prop")
|
||||
.withUsername("postgres")
|
||||
.withPassword("pass")
|
||||
.withExposedPorts(5432);
|
||||
|
||||
postgres.start();
|
||||
String jdbcUrl = String.format("jdbc:postgresql://localhost:%d/prop", postgres.getFirstMappedPort());
|
||||
System.setProperty("spring.datasource.url", jdbcUrl);
|
||||
System.setProperty("spring.datasource.username", "postgres");
|
||||
System.setProperty("spring.datasource.password", "pass");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterAll(ExtensionContext context) {
|
||||
postgres.stop();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
Loading…
Reference in New Issue