/
NikolayIvkin
/
tutorials1
Обзор
Документация
Войти
/
NikolayIvkin
/
tutorials1
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
testing-modules/spring-testing-2/src/test/java/com/baeldung/dynamicproperties/ArticleLiveTest.java
50 строк
2 KB
Mona Mohamadinia
BAEL-4530: A Guide to @DynamicPropertySource in Spring (#9877)
31 авг 2020, 18:43
31 авг 2020, 18:43
25b1749
Код
Авторство
О чём код?
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..."); } }