-
Notifications
You must be signed in to change notification settings - Fork 26
Add TestContainers sample for Doma Spring Boot #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
|
This is not expected work requested in #290 Here is the document for Testcontainers support in Spring Boot. You should create a project from Spring Initializr with "Testcontainers" dependency included. |
…rs support Co-Authored-By: [email protected] <[email protected]>
Co-Authored-By: [email protected] <[email protected]>
making
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, create a project with Spring Initializr using maybe curl and leverage the generated files.
| import org.testcontainers.utility.DockerImageName; | ||
|
|
||
| @TestConfiguration(proxyBeanMethods = false) | ||
| public class TestContainersConfig { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a project with Spring Initializr.
And use TestcontainersConfiguration class included in the generated project.
| @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) | ||
| @ContextConfiguration(classes = TestcontainersConfiguration.class) | ||
| @ActiveProfiles("test") | ||
| @Testcontainers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a project with Spring Initializr.
And import TestcontainersConfiguration class included in the generated project instead of @Testcontainers and @Container.
| @Container | ||
| static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15"); | ||
|
|
||
| @DynamicPropertySource |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DynamicPropertySource is no longer needed as JdbcConnectionDetails is provided via Testconatiners support.
| <artifactId>junit-jupiter</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <!-- TestContainers dependencies --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TestContainers dependencies are managed by Spring Boot
| <artifactId>postgresql</artifactId> | ||
| <scope>runtime</scope> | ||
| </dependency> | ||
| <!-- H2 for local development --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks to Testcontainers support. PostgreSQL can be used for local development as well.
H2 is no longer needed in this project.
| @Import(TestcontainersConfiguration.class) | ||
| class ApplicationTest { | ||
| @Autowired | ||
| private TestRestTemplate restTemplate; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modernize the test code by using RestClient instead of TestRestTemplate.
RestClient restClient;
@BeforeEach
void setUp(@Autowired RestClient.Builder restClientBuilder) {
this.restClient = restClientBuilder.defaultStatusHandler(__ -> true, (req, res) -> {
}).build();
}…e H2, simplify dependencies Co-Authored-By: [email protected] <[email protected]>
|
|
||
| # Doma configuration | ||
| doma.dialect=H2 | ||
| doma.dialect=POSTGRES |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doma.dialect should be automatically detected via JdbcConnectionDetails
| @BeforeEach | ||
| void setUp(@Autowired RestClient.Builder restClientBuilder) { | ||
| this.restClient = restClientBuilder.defaultStatusHandler(__ -> true, (req, res) -> { | ||
| }).build(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use .baseUrl("http://localhost:" + port) to build the RestClient and use .uri("/?text={text}", "hello") for each test method
…ct, improve RestClient usage Co-Authored-By: [email protected] <[email protected]>
| .uri(UriComponentsBuilder.fromUriString("http://localhost").port(port) | ||
| .queryParam("page", "1").queryParam("size", "1").build() | ||
| .toUri()) | ||
| .uri("/?page={page}&size={size}", "1", "1") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
page and size should be int
|
|
||
| # Doma configuration | ||
| doma.naming=SNAKE_LOWER_CASE | ||
| logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DataSourceTransactionManager is old. Use JdbcTransactionManager instead.
logging.level.org.springframework.jdbc.support.JdbcTransactionManager=DEBUG
| @Import(TestcontainersConfiguration.class) | ||
| class ApplicationTest { | ||
| private RestClient restClient; | ||
| private final ParameterizedTypeReference<List<Message>> typedReference = new ParameterizedTypeReference<List<Message>>() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explicit type argument List<Message> can be replaced with <>
| ### Build and Run Tests | ||
|
|
||
| ```bash | ||
| ./mvnw clean test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add maven wrapper
| @@ -0,0 +1,2 @@ | |||
| # Test configuration for PostgreSQL | |||
| spring.sql.init.mode=always | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
redundant file
…t.properties Co-Authored-By: [email protected] <[email protected]>
|
|
||
| ### Build and Run Tests | ||
|
|
||
| This project uses the Maven Wrapper, so you don't need to install Maven separately. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, no. Include .mvn, mvnw and mvnw.cmd in this repo
|
|
||
| # Doma configuration | ||
| doma.naming=SNAKE_LOWER_CASE | ||
| logging.level.org.springframework.jdbc.support.JdbcTransactionManager=DEBUG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add logging.level.org.seasar.doma.jdbc=DEBUG to show SQL logs
| @@ -0,0 +1,6 @@ | |||
| # Development configuration using TestContainers | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is useless
| <artifactId>spring-boot-starter-test</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
junit is managed by Spring Boot
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use path attriubte
…explicit JUnit dependency, use path attribute in RequestMapping Co-Authored-By: [email protected] <[email protected]>
| <artifactId>junit-jupiter</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <!-- JUnit is managed by Spring Boot --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this comment
Co-Authored-By: [email protected] <[email protected]>
| <scope>test</scope> | ||
| </dependency> | ||
| <!-- JUnit is managed by Spring Boot --> | ||
| <!-- Spring Boot manages TestContainers dependencies --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this comment as well
Co-Authored-By: [email protected] <[email protected]>
| - Spring Boot's native TestContainers integration with `@ServiceConnection` | ||
| - Using TestContainers at development time with `SpringApplication.from()` | ||
| - Running Doma queries against a real PostgreSQL database in tests | ||
| - Configuration for both development (H2) and test (PostgreSQL) environments |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
H2 is not used
| <groupId>org.springframework.boot</groupId> | ||
| <artifactId>spring-boot-starter-web</artifactId> | ||
| </dependency> | ||
| <!-- PostgreSQL Driver --> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this comment
making
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Good Job!
Add TestContainers sample for Doma Spring Boot
This PR adds a new sample project that demonstrates how to use TestContainers with Doma Spring Boot.
Features
Fixes #290
Link to Devin run: https://app.devin.ai/sessions/50e457610bac4eae8a933380520d72b8
Requested by: [email protected]