From 1ff29cf596df437780d59ec42f454e556758d402 Mon Sep 17 00:00:00 2001 From: Haroon Khan Date: Wed, 1 Dec 2021 22:40:29 +0000 Subject: [PATCH] [JAVA-8703] Fix Live test to allow isolated execution --- testing-modules/spring-testing-2/pom.xml | 2 +- .../dynamicproperties/ArticleTestFixtureLiveTest.java | 11 +++++++++-- .../dynamicproperties/PostgreSQLExtension.java | 10 +++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/testing-modules/spring-testing-2/pom.xml b/testing-modules/spring-testing-2/pom.xml index 82a9ed9599..6bdec33d96 100644 --- a/testing-modules/spring-testing-2/pom.xml +++ b/testing-modules/spring-testing-2/pom.xml @@ -63,7 +63,7 @@ - 1.12.2 + 1.16.2 \ No newline at end of file diff --git a/testing-modules/spring-testing-2/src/test/java/com/baeldung/dynamicproperties/ArticleTestFixtureLiveTest.java b/testing-modules/spring-testing-2/src/test/java/com/baeldung/dynamicproperties/ArticleTestFixtureLiveTest.java index bb3ad28365..2975104c14 100644 --- a/testing-modules/spring-testing-2/src/test/java/com/baeldung/dynamicproperties/ArticleTestFixtureLiveTest.java +++ b/testing-modules/spring-testing-2/src/test/java/com/baeldung/dynamicproperties/ArticleTestFixtureLiveTest.java @@ -4,13 +4,17 @@ 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.annotation.DirtiesContext; import org.springframework.test.context.ActiveProfiles; +import java.util.List; + import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest @ActiveProfiles("pg") @ExtendWith(PostgreSQLExtension.class) +@DirtiesContext public class ArticleTestFixtureLiveTest { @Autowired @@ -23,8 +27,11 @@ public class ArticleTestFixtureLiveTest { article.setContent("Today's applications..."); articleRepository.save(article); - Article persisted = articleRepository.findAll().get(0); - assertThat(persisted.getId()).isNotNull(); + + List
allArticles = articleRepository.findAll(); + assertThat(allArticles).hasSize(1); + + Article persisted = allArticles.get(0); assertThat(persisted.getTitle()).isEqualTo("A Guide to @DynamicPropertySource in Spring"); assertThat(persisted.getContent()).isEqualTo("Today's applications..."); } diff --git a/testing-modules/spring-testing-2/src/test/java/com/baeldung/dynamicproperties/PostgreSQLExtension.java b/testing-modules/spring-testing-2/src/test/java/com/baeldung/dynamicproperties/PostgreSQLExtension.java index 8c08ad67d7..28aab34867 100644 --- a/testing-modules/spring-testing-2/src/test/java/com/baeldung/dynamicproperties/PostgreSQLExtension.java +++ b/testing-modules/spring-testing-2/src/test/java/com/baeldung/dynamicproperties/PostgreSQLExtension.java @@ -18,14 +18,14 @@ public class PostgreSQLExtension implements BeforeAllCallback, AfterAllCallback .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"); + + System.setProperty("spring.datasource.url", postgres.getJdbcUrl()); + System.setProperty("spring.datasource.username", postgres.getUsername()); + System.setProperty("spring.datasource.password", postgres.getPassword()); } @Override public void afterAll(ExtensionContext context) { - postgres.stop(); + // do nothing, Testcontainers handles container shutdown } }