Skip to content

Commit aad9a5a

Browse files
committed
Migrating to Spring Boot 3.2
1 parent f51a43b commit aad9a5a

File tree

4 files changed

+31
-36
lines changed

4 files changed

+31
-36
lines changed

build.gradle.kts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
plugins {
2-
id("org.springframework.boot") version "3.0.0-M3"
3-
id("io.spring.dependency-management") version "1.0.11.RELEASE"
2+
id("org.springframework.boot") version "3.2.0"
3+
id("io.spring.dependency-management") version "1.1.4"
44
java
55
}
66

@@ -15,11 +15,10 @@ configurations {
1515

1616
repositories {
1717
mavenCentral()
18-
maven(url = "https://repo.spring.io/milestone")
1918
}
2019

2120
ext {
22-
set("testcontainersVersion", "1.17.3")
21+
set("testcontainersVersion", "1.19.3")
2322
}
2423

2524
dependencies {
@@ -31,6 +30,7 @@ dependencies {
3130
compileOnly("org.projectlombok:lombok")
3231
runtimeOnly("org.postgresql:postgresql")
3332
annotationProcessor("org.projectlombok:lombok")
33+
testImplementation("org.springframework.boot:spring-boot-testcontainers")
3434
testImplementation("org.springframework.boot:spring-boot-starter-test")
3535
testImplementation("org.testcontainers:junit-jupiter")
3636
testImplementation("org.testcontainers:postgresql")

settings.gradle.kts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/main/java/com/example/person/api/PersonController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public ResponseEntity<PersonResponse> findOnePerson(@PathVariable UUID personId)
6060

6161
@ExceptionHandler(PersonNotFoundException.class)
6262
public ResponseEntity<ProblemDetail> personNotFoundException(PersonNotFoundException exception) {
63-
final var responseBody = ProblemDetail.forStatus(404)
64-
.withTitle("Person not found by ID")
65-
.withDetail("No person with ID " + exception.getPersonId() + " exists");
63+
final var responseBody = ProblemDetail.forStatus(404);
64+
responseBody.setTitle("Person not found by ID");
65+
responseBody.setDetail("No person with ID " + exception.getPersonId() + " exists");
6666

6767
return ResponseEntity.status(responseBody.getStatus())
6868
.body(responseBody);

src/test/java/com/example/ApplicationTest.java

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212
import org.junit.jupiter.params.provider.NullAndEmptySource;
1313
import org.junit.jupiter.params.provider.ValueSource;
1414
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionDetails;
16+
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
1517
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
1618
import org.springframework.boot.test.context.SpringBootTest;
19+
import org.springframework.boot.test.context.TestConfiguration;
20+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Import;
1723
import org.springframework.http.HttpHeaders;
1824
import org.springframework.http.HttpStatus;
1925
import org.springframework.http.MediaType;
20-
import org.springframework.test.context.DynamicPropertyRegistry;
21-
import org.springframework.test.context.DynamicPropertySource;
2226
import org.springframework.test.web.reactive.server.EntityExchangeResult;
2327
import org.springframework.test.web.reactive.server.WebTestClient;
2428
import org.springframework.web.reactive.function.BodyInserters;
2529
import org.testcontainers.containers.GenericContainer;
2630
import org.testcontainers.containers.PostgreSQLContainer;
27-
import org.testcontainers.junit.jupiter.Container;
2831
import org.testcontainers.junit.jupiter.Testcontainers;
2932
import org.testcontainers.utility.DockerImageName;
3033

@@ -34,29 +37,12 @@
3437
import static org.junit.jupiter.api.Assertions.assertNotNull;
3538

3639
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
40+
@Import(ApplicationTest.TestConfig.class)
3741
@AutoConfigureWebTestClient
3842
@Testcontainers
3943
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
4044
class ApplicationTest {
4145

42-
@Container
43-
private static final PostgreSQLContainer<?> POSTGRES_CONTAINER =
44-
new PostgreSQLContainer<>(DockerImageName.parse("postgres"));
45-
46-
@Container
47-
private static final GenericContainer<?> REDIS_CONTAINER =
48-
new GenericContainer<>(DockerImageName.parse("redis"))
49-
.withExposedPorts(6379);
50-
51-
@DynamicPropertySource
52-
private static void setApplicationProperties(DynamicPropertyRegistry registry) {
53-
registry.add("spring.datasource.url", POSTGRES_CONTAINER::getJdbcUrl);
54-
registry.add("spring.datasource.username", POSTGRES_CONTAINER::getUsername);
55-
registry.add("spring.datasource.password", POSTGRES_CONTAINER::getPassword);
56-
registry.add("spring.redis.host", REDIS_CONTAINER::getHost);
57-
registry.add("spring.redis.port", REDIS_CONTAINER::getFirstMappedPort);
58-
}
59-
6046
private static PersonRequest personRequest;
6147
private static UUID personId;
6248

@@ -184,4 +170,21 @@ private UUID getPersonIdFromLocationHeader(EntityExchangeResult<Void> exchangeRe
184170

185171
return UUID.fromString(lastSegment);
186172
}
173+
174+
@TestConfiguration
175+
static class TestConfig {
176+
177+
@Bean
178+
@ServiceConnection(type = JdbcConnectionDetails.class)
179+
PostgreSQLContainer<?> POSTGRES_CONTAINER() {
180+
return new PostgreSQLContainer<>(DockerImageName.parse("postgres"));
181+
}
182+
183+
@Bean
184+
@ServiceConnection(name = "redis", type = RedisConnectionDetails.class)
185+
GenericContainer<?> REDIS_CONTAINER() {
186+
return new GenericContainer<>(DockerImageName.parse("redis"))
187+
.withExposedPorts(6379);
188+
}
189+
}
187190
}

0 commit comments

Comments
 (0)