[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> </build>
<properties> <properties>
<testcontainers.version>1.12.2</testcontainers.version> <testcontainers.version>1.16.2</testcontainers.version>
</properties> </properties>
</project> </project>

View File

@ -4,13 +4,17 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest @SpringBootTest
@ActiveProfiles("pg") @ActiveProfiles("pg")
@ExtendWith(PostgreSQLExtension.class) @ExtendWith(PostgreSQLExtension.class)
@DirtiesContext
public class ArticleTestFixtureLiveTest { public class ArticleTestFixtureLiveTest {
@Autowired @Autowired
@ -23,8 +27,11 @@ public class ArticleTestFixtureLiveTest {
article.setContent("Today's applications..."); article.setContent("Today's applications...");
articleRepository.save(article); 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.getTitle()).isEqualTo("A Guide to @DynamicPropertySource in Spring");
assertThat(persisted.getContent()).isEqualTo("Today's applications..."); assertThat(persisted.getContent()).isEqualTo("Today's applications...");
} }

View File

@ -18,14 +18,14 @@ public class PostgreSQLExtension implements BeforeAllCallback, AfterAllCallback
.withExposedPorts(5432); .withExposedPorts(5432);
postgres.start(); postgres.start();
String jdbcUrl = String.format("jdbc:postgresql://localhost:%d/prop", postgres.getFirstMappedPort());
System.setProperty("spring.datasource.url", jdbcUrl); System.setProperty("spring.datasource.url", postgres.getJdbcUrl());
System.setProperty("spring.datasource.username", "postgres"); System.setProperty("spring.datasource.username", postgres.getUsername());
System.setProperty("spring.datasource.password", "pass"); System.setProperty("spring.datasource.password", postgres.getPassword());
} }
@Override @Override
public void afterAll(ExtensionContext context) { public void afterAll(ExtensionContext context) {
postgres.stop(); // do nothing, Testcontainers handles container shutdown
} }
} }