From ef383093b5b51cf99cced3c9a4cbcf7d7abb94b8 Mon Sep 17 00:00:00 2001 From: Tim Sparg Date: Fri, 26 Jul 2024 14:46:13 +0200 Subject: [PATCH] Fix Spring Data Querydsl example. The io.github.jpenren:thymeleaf-spring-data-dialect dependency was out of sync with thymeleaf causing an error when you tried to access the index page. Additionally Added a TestApplication and revamped the TestContainers usage. --- web/querydsl/README.md | 5 +-- web/querydsl/pom.xml | 18 +++++++-- .../src/main/resources/templates/index.html | 14 ++++--- .../java/example/users/ApplicationTests.java | 28 +++----------- .../java/example/users/TestApplication.java | 29 ++++++++++++++ .../users/TestcontainersConfiguration.java | 38 +++++++++++++++++++ 6 files changed, 98 insertions(+), 34 deletions(-) create mode 100644 web/querydsl/src/test/java/example/users/TestApplication.java create mode 100644 web/querydsl/src/test/java/example/users/TestcontainersConfiguration.java diff --git a/web/querydsl/README.md b/web/querydsl/README.md index b8aab84c5..91ae105c5 100644 --- a/web/querydsl/README.md +++ b/web/querydsl/README.md @@ -4,9 +4,8 @@ This example shows some of the Spring Data Querydsl integration features with Sp ## Quickstart -1. Install MongoDB (http://www.mongodb.org/downloads, unzip, run `mkdir data`, run `bin/mongod --dbpath=data`) -2. Build and run the app (`mvn spring-boot:run`) -4. Access app directly via its UI (`http://localhost:8080/`). +1. Build and run the app (`mvn spring-boot:test-run`) +2. Access app directly via its UI (`http://localhost:8080/`). ## Interesting bits diff --git a/web/querydsl/pom.xml b/web/querydsl/pom.xml index 36ab136b2..d2a1c4991 100644 --- a/web/querydsl/pom.xml +++ b/web/querydsl/pom.xml @@ -58,20 +58,20 @@ io.github.jpenren thymeleaf-spring-data-dialect - 3.3.0 + 3.6.0 org.webjars jquery - 2.1.3 + 3.7.1 runtime org.webjars bootstrap - 3.3.4 + 5.3.3 runtime @@ -94,6 +94,18 @@ test + + org.testcontainers + junit-jupiter + test + + + + org.springframework.boot + spring-boot-testcontainers + test + + diff --git a/web/querydsl/src/main/resources/templates/index.html b/web/querydsl/src/main/resources/templates/index.html index 6e59b3e99..1984d043a 100644 --- a/web/querydsl/src/main/resources/templates/index.html +++ b/web/querydsl/src/main/resources/templates/index.html @@ -23,23 +23,27 @@

Search

- +
- +
- +
- +
- +
diff --git a/web/querydsl/src/test/java/example/users/ApplicationTests.java b/web/querydsl/src/test/java/example/users/ApplicationTests.java index 594ea2331..0f83c0706 100644 --- a/web/querydsl/src/test/java/example/users/ApplicationTests.java +++ b/web/querydsl/src/test/java/example/users/ApplicationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2017-2021 the original author or authors. + * Copyright 2017-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,44 +15,26 @@ */ package example.users; - import org.junit.jupiter.api.Test; - import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.DynamicPropertyRegistry; -import org.springframework.test.context.DynamicPropertySource; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; import org.testcontainers.containers.MongoDBContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.utility.DockerImageName; /** * @author Oliver Gierke * @author Divya Srivastava + * @author Tim Sparg */ @Testcontainers @SpringBootTest class ApplicationTests { - @Container // - private static MongoDBContainer mongoDBContainer = MongoContainers.getDefaultContainer(); - - @DynamicPropertySource - static void setProperties(DynamicPropertyRegistry registry) { - registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl); - } + @Container + @ServiceConnection static MongoDBContainer mongoDBContainer = new MongoDBContainer("mongo:7"); @Test void contextBootstraps() {} - static class MongoContainers { - - private static final String IMAGE_NAME = "mongo:5.0"; - private static final String IMAGE_NAME_PROPERTY = "mongo.default.image.name"; - - public static MongoDBContainer getDefaultContainer() { - return new MongoDBContainer(DockerImageName.parse(System.getProperty(IMAGE_NAME_PROPERTY, IMAGE_NAME))) - .withReuse(true); - } - } } diff --git a/web/querydsl/src/test/java/example/users/TestApplication.java b/web/querydsl/src/test/java/example/users/TestApplication.java new file mode 100644 index 000000000..15f6c4842 --- /dev/null +++ b/web/querydsl/src/test/java/example/users/TestApplication.java @@ -0,0 +1,29 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.users; + +import org.springframework.boot.SpringApplication; + +/** + * @author Tim Sparg + */ +public class TestApplication { + + public static void main(String[] args) { + SpringApplication.from(Application::main).with(TestcontainersConfiguration.class).run(args); + } + +} diff --git a/web/querydsl/src/test/java/example/users/TestcontainersConfiguration.java b/web/querydsl/src/test/java/example/users/TestcontainersConfiguration.java new file mode 100644 index 000000000..0749dc685 --- /dev/null +++ b/web/querydsl/src/test/java/example/users/TestcontainersConfiguration.java @@ -0,0 +1,38 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package example.users; + +import org.springframework.boot.devtools.restart.RestartScope; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.context.annotation.Bean; +import org.testcontainers.containers.MongoDBContainer; +import org.testcontainers.utility.DockerImageName; + +/** + * @author Tim Sparg + */ +@TestConfiguration(proxyBeanMethods = false) +public class TestcontainersConfiguration { + + @Bean + @ServiceConnection + @RestartScope + MongoDBContainer mongoDbContainer() { + return new MongoDBContainer(DockerImageName.parse("mongo:7")); + } + +}