diff --git a/poc-rest-api/spring-boot-rest-webmvc/pom.xml b/poc-rest-api/spring-boot-rest-webmvc/pom.xml index 9e5bb1d17..984f6ef44 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/pom.xml +++ b/poc-rest-api/spring-boot-rest-webmvc/pom.xml @@ -7,7 +7,7 @@ org.springframework.boot spring-boot-starter-parent - 3.5.11 + 4.0.3 @@ -18,14 +18,13 @@ 21 1.6.3 - 2.8.15 + 3.0.1 + + 1.0.0 3.13.0 12.2.0 3.2.1 - - local - local @@ -43,7 +42,7 @@ org.springframework.boot - spring-boot-starter-web + spring-boot-starter-webmvc org.springframework.boot @@ -66,8 +65,8 @@ caffeine - org.liquibase - liquibase-core + org.springframework.boot + spring-boot-starter-liquibase org.mapstruct @@ -102,22 +101,37 @@ org.springframework.boot - spring-boot-starter-test + spring-boot-starter-webmvc-test test - org.springframework.batch - spring-batch-test + org.springframework.boot + spring-boot-starter-batch-test + test + + + org.springframework.boot + spring-boot-resttestclient + test + + + org.springframework.boot + spring-boot-starter-data-jpa-test + test + + + org.springframework.boot + spring-boot-starter-restclient test org.testcontainers - junit-jupiter + testcontainers-junit-jupiter test org.testcontainers - postgresql + testcontainers-postgresql test @@ -215,113 +229,81 @@ false - - - org.codehaus.gmaven - groovy-maven-plugin - 2.1.1 - - - generate-sources - - execute - - - - db = new org.testcontainers.containers.PostgreSQLContainer("postgres:latest") - .withUsername("${db.username}") - .withDatabaseName("rest") - .withPassword("${db.password}"); - db.start(); - project.properties.setProperty('db.url', db.getJdbcUrl()); - - - - + + org.apache.maven.plugins + maven-surefire-plugin + + + dev.sivalabs + testcontainers-jooq-codegen-maven-plugin + ${testcontainers-jooq-codegen-maven-plugin.version} org.testcontainers - postgresql + testcontainers-postgresql ${testcontainers.version} - - - - - org.flywaydb - flyway-maven-plugin - ${flyway.version} - - - generate-sources - - migrate - - - ${db.url} - ${db.username} - ${db.password} - - filesystem:src/main/resources/db/migration - - - - - - org.flywaydb - flyway-database-postgresql - ${flyway.version} + org.postgresql + postgresql + ${postgresql.version} + + + org.jooq + jooq-codegen + ${jooq.version} - - - - org.jooq - jooq-codegen-maven - - java-generator - generate-sources + generate-jooq-sources generate - + generate-sources - - ${db.url} - ${db.username} - ${db.password} - - - - public - - - com.example.poc.webmvc.testcontainersflyway.db - target/generated-sources/jooq - - + + + POSTGRES + + postgres:18.2-alpine + + + + filesystem:src/main/resources/db/migration + + + + + + + .* + DATABASECHANGELOG.* + public + + + com.example.poc.webmvc.db + target/generated-sources/jooq + + + - - - org.apache.maven.plugins - maven-surefire-plugin - - - ${db.url} - ${db.username} - ${db.password} - - - com.diffplug.spotless spotless-maven-plugin @@ -329,7 +311,7 @@ - 1.25.2 + 1.28.0 @@ -349,25 +331,4 @@ - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/SpringBootRestWebMvcApplication.java b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/SpringBootRestWebMvcApplication.java index 6f2689075..78ed3c9db 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/SpringBootRestWebMvcApplication.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/SpringBootRestWebMvcApplication.java @@ -4,8 +4,8 @@ import com.example.poc.webmvc.config.ApplicationProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.persistence.autoconfigure.EntityScan; @SpringBootApplication @EntityScan(basePackages = {"com.example.poc.webmvc.entities"}) diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/CustomItemProcessor.java b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/CustomItemProcessor.java index 53662a328..1ab2d2a4b 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/CustomItemProcessor.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/CustomItemProcessor.java @@ -11,7 +11,7 @@ import java.util.stream.Collector; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; -import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.infrastructure.item.ItemProcessor; import org.springframework.stereotype.Component; @RequiredArgsConstructor diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/CustomItemReader.java b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/CustomItemReader.java index 69d2b832c..cd5e4d3d3 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/CustomItemReader.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/CustomItemReader.java @@ -7,28 +7,25 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; -import lombok.RequiredArgsConstructor; -import org.springframework.batch.core.JobParameters; -import org.springframework.batch.core.StepExecution; -import org.springframework.batch.core.annotation.BeforeStep; -import org.springframework.batch.item.database.AbstractPagingItemReader; +import org.springframework.batch.core.configuration.annotation.StepScope; +import org.springframework.batch.infrastructure.item.database.AbstractPagingItemReader; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import org.springframework.util.CollectionUtils; -@RequiredArgsConstructor @Component +@StepScope public class CustomItemReader extends AbstractPagingItemReader> { private final PostRepository postRepository; private List> ids = new ArrayList<>(); + @Value("#{jobParameters['key']}") private String titleValue; - @BeforeStep - public void beforeStep(final StepExecution stepExecution) { - JobParameters parameters = stepExecution.getJobExecution().getJobParameters(); - // use your parameters - this.titleValue = parameters.getString("key"); + public CustomItemReader(PostRepository postRepository) { + this.postRepository = postRepository; } @Override @@ -52,6 +49,10 @@ protected void doReadPage() { / getPageSize())) .values()); } - results.add(this.ids.get(getPage() * getPageSize())); + if (!CollectionUtils.isEmpty(ids)) { + if (getPage() < this.ids.size()) { + results.add(this.ids.get(getPage())); + } + } } } diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/CustomItemWriter.java b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/CustomItemWriter.java index 455458bd7..6c210125b 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/CustomItemWriter.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/CustomItemWriter.java @@ -4,8 +4,8 @@ import com.example.poc.webmvc.dto.PostDTO; import java.util.List; import lombok.extern.slf4j.Slf4j; -import org.springframework.batch.item.Chunk; -import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.infrastructure.item.Chunk; +import org.springframework.batch.infrastructure.item.ItemWriter; import org.springframework.stereotype.Component; @Component diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/JobInvokerController.java b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/JobInvokerController.java index a6517973d..41fbff807 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/JobInvokerController.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/JobInvokerController.java @@ -3,11 +3,11 @@ import java.util.Date; import lombok.RequiredArgsConstructor; -import org.springframework.batch.core.Job; -import org.springframework.batch.core.JobExecution; -import org.springframework.batch.core.JobParameters; -import org.springframework.batch.core.JobParametersBuilder; -import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.job.Job; +import org.springframework.batch.core.job.JobExecution; +import org.springframework.batch.core.job.parameters.JobParameters; +import org.springframework.batch.core.job.parameters.JobParametersBuilder; +import org.springframework.batch.core.launch.JobOperator; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @@ -15,7 +15,7 @@ @RequiredArgsConstructor public class JobInvokerController { - private final JobLauncher jobLauncher; + private final JobOperator jobOperator; private final Job executionJob; @@ -27,8 +27,8 @@ public String handle() throws Exception { .addString("key", "Post") .addDate("currentDate", new Date()) .toJobParameters(); - JobExecution jobExecution = this.jobLauncher.run(this.executionJob, jobParameters); + JobExecution jobExecution = this.jobOperator.start(this.executionJob, jobParameters); - return "Batch job has been invoked as " + jobExecution.getJobId(); + return "Batch job has been invoked as " + jobExecution.getJobInstanceId(); } } diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/ReportsExecutionJob.java b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/ReportsExecutionJob.java index e591e26bf..85d6481f7 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/ReportsExecutionJob.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/batch/ReportsExecutionJob.java @@ -3,13 +3,15 @@ import com.example.poc.webmvc.dto.PostDTO; import java.util.List; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.batch.core.*; -import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.job.Job; +import org.springframework.batch.core.job.JobExecution; import org.springframework.batch.core.job.builder.JobBuilder; -import org.springframework.batch.core.launch.support.RunIdIncrementer; +import org.springframework.batch.core.job.parameters.RunIdIncrementer; +import org.springframework.batch.core.listener.JobExecutionListener; import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.step.Step; import org.springframework.batch.core.step.builder.StepBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -17,8 +19,6 @@ @Slf4j @Configuration -@EnableBatchProcessing -@RequiredArgsConstructor public class ReportsExecutionJob implements JobExecutionListener { @Bean(name = "executionJob") @@ -32,14 +32,14 @@ public Job reportsExecutionJob( Step step = new StepBuilder("execution-step", jobRepository) .allowStartIfComplete(true) - ., List>chunk(2, transactionManager) + ., List>chunk(2) .reader(reader) .processor(processor) .writer(writer) + .transactionManager(transactionManager) .build(); return new JobBuilder("reporting-job", jobRepository) - .start(step) .incrementer(new RunIdIncrementer()) .listener(this) .start(step) diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/config/WebMvcConfig.java b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/config/WebMvcConfig.java index c6077b972..cdc0947d0 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/config/WebMvcConfig.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/config/WebMvcConfig.java @@ -1,8 +1,8 @@ /* Licensed under Apache-2.0 2025 */ package com.example.poc.webmvc.config; +import org.jspecify.annotations.NonNull; import org.springframework.context.annotation.Configuration; -import org.springframework.lang.NonNull; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/exception/ApiError.java b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/exception/ApiError.java index 76e688135..8b972a315 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/exception/ApiError.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/exception/ApiError.java @@ -3,7 +3,6 @@ import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonTypeInfo; -import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver; import jakarta.validation.ConstraintViolation; import java.time.LocalDateTime; import java.util.ArrayList; @@ -17,6 +16,7 @@ import org.springframework.http.HttpStatusCode; import org.springframework.validation.FieldError; import org.springframework.validation.ObjectError; +import tools.jackson.databind.annotation.JsonTypeIdResolver; @Data @JsonTypeInfo( diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/exception/LowerCaseClassNameResolver.java b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/exception/LowerCaseClassNameResolver.java index 7e45b3c3a..ae5d3729a 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/exception/LowerCaseClassNameResolver.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/exception/LowerCaseClassNameResolver.java @@ -2,21 +2,22 @@ package com.example.poc.webmvc.exception; import com.fasterxml.jackson.annotation.JsonTypeInfo; -import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase; import java.util.Locale; +import tools.jackson.core.JacksonException; +import tools.jackson.databind.DatabindContext; +import tools.jackson.databind.jsontype.impl.TypeIdResolverBase; class LowerCaseClassNameResolver extends TypeIdResolverBase { - /** {@inheritDoc} */ @Override - public String idFromValue(Object value) { + public String idFromValue(DatabindContext ctxt, Object value) throws JacksonException { return value.getClass().getSimpleName().toLowerCase(Locale.getDefault()); } - /** {@inheritDoc} */ @Override - public String idFromValueAndType(Object value, Class suggestedType) { - return idFromValue(value); + public String idFromValueAndType(DatabindContext ctxt, Object value, Class suggestedType) + throws JacksonException { + return idFromValue(ctxt, value); } /** {@inheritDoc} */ diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/service/impl/JooqPostServiceImpl.java b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/service/impl/JooqPostServiceImpl.java index c1339d3ff..480584137 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/service/impl/JooqPostServiceImpl.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/main/java/com/example/poc/webmvc/service/impl/JooqPostServiceImpl.java @@ -1,11 +1,11 @@ /* Licensed under Apache-2.0 2025 */ package com.example.poc.webmvc.service.impl; -import static com.example.poc.webmvc.testcontainersflyway.db.tables.Post.POST; -import static com.example.poc.webmvc.testcontainersflyway.db.tables.PostComment.POST_COMMENT; -import static com.example.poc.webmvc.testcontainersflyway.db.tables.PostDetails.POST_DETAILS; -import static com.example.poc.webmvc.testcontainersflyway.db.tables.PostTag.POST_TAG; -import static com.example.poc.webmvc.testcontainersflyway.db.tables.Tag.TAG; +import static com.example.poc.webmvc.db.tables.Post.POST; +import static com.example.poc.webmvc.db.tables.PostComment.POST_COMMENT; +import static com.example.poc.webmvc.db.tables.PostDetails.POST_DETAILS; +import static com.example.poc.webmvc.db.tables.PostTag.POST_TAG; +import static com.example.poc.webmvc.db.tables.Tag.TAG; import static org.jooq.Records.mapping; import static org.jooq.impl.DSL.multiset; import static org.jooq.impl.DSL.select; @@ -82,7 +82,7 @@ public JooqPostServiceImpl(DSLContext dsl) { * @return list of posts */ @Override - @Cacheable(value = "posts", key = "#userName", unless = "#result == null") + @Cacheable(value = "postsByUser", key = "#userName", unless = "#result == null") public List fetchAllPostsByUserName(String userName) { return dsl.select( @@ -112,7 +112,10 @@ public List fetchAllPostsByUserName(String userName) { } @Override - @Cacheable(value = "posts", key = "#userName+#title", unless = "#result == null") + @Cacheable( + value = "postByUserAndTitle", + key = "#userName + ':' + #title", + unless = "#result == null") public PostDTO fetchPostByUserNameAndTitle(String userName, String title) { return dsl.select( diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/batch/SpringBatchIntegrationTest.java b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/batch/SpringBatchIntegrationTest.java index d84a540f3..35df80d95 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/batch/SpringBatchIntegrationTest.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/batch/SpringBatchIntegrationTest.java @@ -5,9 +5,15 @@ import com.example.poc.webmvc.common.AbstractIntegrationTest; import java.util.Date; -import org.junit.jupiter.api.*; -import org.springframework.batch.core.*; -import org.springframework.batch.test.JobLauncherTestUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.job.Job; +import org.springframework.batch.core.job.JobExecution; +import org.springframework.batch.core.job.JobInstance; +import org.springframework.batch.core.job.parameters.JobParameters; +import org.springframework.batch.core.job.parameters.JobParametersBuilder; +import org.springframework.batch.test.JobOperatorTestUtils; import org.springframework.batch.test.JobRepositoryTestUtils; import org.springframework.batch.test.context.SpringBatchTest; import org.springframework.beans.factory.annotation.Autowired; @@ -17,7 +23,7 @@ @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) class SpringBatchIntegrationTest extends AbstractIntegrationTest { - @Autowired private JobLauncherTestUtils jobLauncherTestUtils; + @Autowired private JobOperatorTestUtils jobLauncherTestUtils; @Autowired private JobRepositoryTestUtils jobRepositoryTestUtils; @Autowired private Job jobUnderTest; diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/common/AbstractIntegrationTest.java b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/common/AbstractIntegrationTest.java index 620782d0b..9260aa1eb 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/common/AbstractIntegrationTest.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/common/AbstractIntegrationTest.java @@ -6,44 +6,37 @@ import java.time.Duration; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.restclient.RestTemplateBuilder; +import org.springframework.boot.restclient.autoconfigure.RestTemplateBuilderConfigurer; +import org.springframework.boot.resttestclient.TestRestTemplate; +import org.springframework.boot.resttestclient.autoconfigure.AutoConfigureTestRestTemplate; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.context.annotation.Bean; -import org.springframework.core.env.Environment; import org.springframework.test.context.ActiveProfiles; @ActiveProfiles({PROFILE_TEST, PROFILE_IT}) +@AutoConfigureTestRestTemplate @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public abstract class AbstractIntegrationTest extends AbstractPostgreSQLContainerBase { - @Autowired private Environment environment; - - protected TestRestTemplate restTemplate() { - return configure(new TestRestTemplate()); - } + @Autowired protected TestRestTemplate restTemplate; protected TestRestTemplate adminRestTemplate() { - return configure(new TestRestTemplate("admin", "admin")); + return restTemplate.withBasicAuth("admin", "admin"); } protected TestRestTemplate userRestTemplate() { - return configure(new TestRestTemplate("username", "password")); - } - - private TestRestTemplate configure(TestRestTemplate restTemplate) { - restTemplate.setUriTemplateHandler(new LocalHostUriTemplateHandler(this.environment)); - return restTemplate; + return restTemplate.withBasicAuth("username", "password"); } @TestConfiguration static class Config { @Bean - RestTemplateBuilder restTemplateBuilder() { - return new RestTemplateBuilder() + RestTemplateBuilder restTemplateBuilder(RestTemplateBuilderConfigurer configurer) { + return configurer + .configure(new RestTemplateBuilder()) .connectTimeout(Duration.ofSeconds(1)) .readTimeout(Duration.ofSeconds(1)); } diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/common/AbstractPostgreSQLContainerBase.java b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/common/AbstractPostgreSQLContainerBase.java index 5ef6d3310..b43401b7a 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/common/AbstractPostgreSQLContainerBase.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/common/AbstractPostgreSQLContainerBase.java @@ -3,14 +3,14 @@ import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; -import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.postgresql.PostgreSQLContainer; public abstract class AbstractPostgreSQLContainerBase { @Container - protected static final PostgreSQLContainer sqlContainer = - new PostgreSQLContainer<>("postgres:18-alpine"); + protected static final PostgreSQLContainer sqlContainer = + new PostgreSQLContainer("postgres:18-alpine"); static { sqlContainer.start(); diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/controller/PingControllerITTest.java b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/controller/PingControllerITTest.java index 6c8f8ea30..0c68baf00 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/controller/PingControllerITTest.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/controller/PingControllerITTest.java @@ -15,7 +15,7 @@ class PingControllerITTest extends AbstractIntegrationTest { @Test void testPingEndpoint() { ResponseEntity response = - restTemplate().getForEntity("/api/ping", PingController.PingResponse.class); + restTemplate.getForEntity("/api/ping", PingController.PingResponse.class); assertThat(response).isNotNull(); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); @@ -28,9 +28,8 @@ void testPingEndpoint() { @Test void testPingWithAuthentication() { ResponseEntity response = - restTemplate() - .getForEntity( - "/api/pingWithAuthentication", PingController.PingResponse.class); + restTemplate.getForEntity( + "/api/pingWithAuthentication", PingController.PingResponse.class); assertThat(response).isNotNull(); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); @@ -50,12 +49,11 @@ void testPingWithAuthenticationHeader() { headers.set(HttpHeaders.AUTHORIZATION, authHeader); ResponseEntity response = - restTemplate() - .exchange( - "/api/pingWithAuthentication", - HttpMethod.GET, - new HttpEntity<>(headers), - PingController.PingResponse.class); + restTemplate.exchange( + "/api/pingWithAuthentication", + HttpMethod.GET, + new HttpEntity<>(headers), + PingController.PingResponse.class); assertThat(response).isNotNull(); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); @@ -73,12 +71,11 @@ void testCorsHeadersForPingEndpoint() { headers.set(HttpHeaders.HOST, "localhost"); ResponseEntity response = - restTemplate() - .exchange( - "/api/ping", - HttpMethod.GET, - new HttpEntity<>(headers), - PingController.PingResponse.class); + restTemplate.exchange( + "/api/ping", + HttpMethod.GET, + new HttpEntity<>(headers), + PingController.PingResponse.class); assertThat(response).isNotNull(); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); @@ -97,12 +94,8 @@ void testCorsPreflightRequest() { headers.set(HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS, "Content-Type,Authorization"); ResponseEntity response = - restTemplate() - .exchange( - "/api/ping", - HttpMethod.OPTIONS, - new HttpEntity<>(headers), - Void.class); + restTemplate.exchange( + "/api/ping", HttpMethod.OPTIONS, new HttpEntity<>(headers), Void.class); assertThat(response).isNotNull(); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/controller/PostControllerITTest.java b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/controller/PostControllerITTest.java index e27155573..4a945c146 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/controller/PostControllerITTest.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/controller/PostControllerITTest.java @@ -13,6 +13,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestMethodOrder; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -22,6 +23,7 @@ import org.springframework.http.ResponseEntity; @TestMethodOrder(MethodOrderer.MethodName.class) +@TestInstance(TestInstance.Lifecycle.PER_CLASS) class PostControllerITTest extends AbstractIntegrationTest { private PostDTO postDto; @@ -46,7 +48,7 @@ void test01_FetchingPostsByUserId() { assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); assertThat(response.getBody()).isNotNull(); assertThat(response.getBody().postList()).isNotEmpty().hasSize(2); - PostDTO postDTO = response.getBody().postList().get(0); + PostDTO postDTO = response.getBody().postList().getFirst(); assertThat(postDTO.getComments()) .isNotEmpty() .hasSize(2) diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/controller/PostControllerTest.java b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/controller/PostControllerTest.java index 4c2aa51fd..3d8426b05 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/controller/PostControllerTest.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/controller/PostControllerTest.java @@ -16,7 +16,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest; import org.springframework.hateoas.MediaTypes; import org.springframework.http.HttpHeaders; import org.springframework.test.context.bean.override.mockito.MockitoBean; diff --git a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/repository/PostRepositoryTest.java b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/repository/PostRepositoryTest.java index 669b1cc43..b249387b2 100644 --- a/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/repository/PostRepositoryTest.java +++ b/poc-rest-api/spring-boot-rest-webmvc/src/test/java/com/example/poc/webmvc/repository/PostRepositoryTest.java @@ -22,8 +22,8 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest; +import org.springframework.boot.jdbc.test.autoconfigure.AutoConfigureTestDatabase; @DataJpaTest @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)