[JAVA-8703] Fix Live test to allow isolated execution

This commit is contained in:
Haroon Khan 2021-12-01 22:40:29 +00:00
parent 36866c09f7
commit 1ff29cf596
3 changed files with 15 additions and 8 deletions

View File

@ -63,7 +63,7 @@
</build>
<properties>
<testcontainers.version>1.12.2</testcontainers.version>
<testcontainers.version>1.16.2</testcontainers.version>
</properties>
</project>

View File

@ -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<Article> 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...");
}

View File

@ -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
}
}