Skip to content

Commit

Permalink
fix: fix testcontainers issues
Browse files Browse the repository at this point in the history
  • Loading branch information
yingtingxu committed Jun 7, 2024
1 parent 9c3d394 commit e78cca6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
2 changes: 1 addition & 1 deletion mall-catalog/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ docker run -d \
-e POSTGRES_PASSWORD=p@ssword \
-e POSTGRES_DB=mall_catalog \
-p 5432:5432 \
postgres:14.10
postgres:16.3-alpine
```

## Container Commands
Expand Down
3 changes: 2 additions & 1 deletion mall-catalog/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ java {
}

ext {
set 'springCloudVersion', "2023.0.2"
set 'springCloudVersion', '2023.0.2'
set 'testcontainers.version', '1.19.8'
}

configurations {
Expand Down
6 changes: 3 additions & 3 deletions mall-catalog/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ spring:
hikari:
connection-timeout: 2000 # 2s
maximum-pool-size: 5
# sql:
# init:
# mode: always
sql:
init:
mode: always
config:
import: "optional:configserver:"
cloud:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,48 @@
package org.mall.catalogservice;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mall.catalogservice.domain.Book;
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.springframework.test.web.reactive.server.WebTestClient;
import org.testcontainers.containers.PostgreSQLContainer;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT
)
@ActiveProfiles("integration")
//@ActiveProfiles("integration")
class ApplicationTests {
@Autowired
private WebTestClient webTestClient;

static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
"postgres:16.3-alpine"
);

@BeforeAll
static void beforeAll() {
postgres.start();
}

@AfterAll
static void afterAll() {
postgres.stop();
}

@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}

@Test
void whenPostRequestThenBookCreated() {
var expectedBook = Book.of("1231231231", "title", "author", 9.90);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.mall.catalogservice.domain;


import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mall.catalogservice.config.DatabaseAuditing;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -9,20 +11,44 @@
import org.springframework.context.annotation.Import;
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;

import static org.assertj.core.api.Assertions.assertThat;

@DataJdbcTest
@Import(DatabaseAuditing.class)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@ActiveProfiles("integration")
//@ActiveProfiles("integration")
class BookRepositoryJdbcTests {
@Autowired
private BookRepository bookRepository;

@Autowired
private JdbcAggregateTemplate jdbcAggregateTemplate;

static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
"postgres:16.3-alpine"
);

@BeforeAll
static void beforeAll() {
postgres.start();
}

@AfterAll
static void afterAll() {
postgres.stop();
}

@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}

@Test
void findBookByIsbnWhenExisting() {
var isbn = "123";
Expand Down

0 comments on commit e78cca6

Please sign in to comment.