-
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
Changes from 1 commit
2a0f96f
448f356
5198bb1
1e42f72
06610a1
45661c4
87b5535
237ea2d
487094d
2a5fa14
ccf7bfc
ccbf51f
cf62c8e
5f6bdc4
5f5aa9a
37dc226
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,7 @@ | ||
| # Development configuration using H2 | ||
| spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1 | ||
| spring.datasource.driver-class-name=org.h2.Driver | ||
| spring.datasource.username=sa | ||
| spring.datasource.password= | ||
| # Development configuration using TestContainers | ||
|
||
| spring.sql.init.mode=always | ||
|
|
||
| # Doma configuration | ||
| doma.dialect=H2 | ||
| doma.dialect=POSTGRES | ||
|
||
| doma.naming=SNAKE_LOWER_CASE | ||
| logging.level.org.springframework.jdbc.datasource.DataSourceTransactionManager=DEBUG | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,49 +4,56 @@ | |
|
|
||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
| import org.springframework.boot.test.web.client.TestRestTemplate; | ||
| import org.springframework.boot.test.web.server.LocalServerPort; | ||
| import org.springframework.context.annotation.Import; | ||
| import org.springframework.core.ParameterizedTypeReference; | ||
| import org.springframework.http.HttpEntity; | ||
| import org.springframework.http.HttpMethod; | ||
| import org.springframework.web.client.RestClient; | ||
| import org.springframework.web.util.UriComponentsBuilder; | ||
|
|
||
| @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) | ||
| @Import(TestcontainersConfiguration.class) | ||
| class ApplicationTest { | ||
| @Autowired | ||
| private TestRestTemplate restTemplate; | ||
| private RestClient restClient; | ||
| private final ParameterizedTypeReference<List<Message>> typedReference = new ParameterizedTypeReference<List<Message>>() { | ||
|
||
| }; | ||
| @LocalServerPort | ||
| private int port; | ||
|
|
||
| @BeforeEach | ||
| void setUp(@Autowired RestClient.Builder restClientBuilder) { | ||
| this.restClient = restClientBuilder.defaultStatusHandler(__ -> true, (req, res) -> { | ||
| }).build(); | ||
|
||
| } | ||
|
|
||
| @Test | ||
| void testWithTestContainers() { | ||
| Message message1 = restTemplate.getForObject( | ||
| UriComponentsBuilder.fromUriString("http://localhost").port(port) | ||
| .queryParam("text", "hello").build().toUri(), | ||
| Message.class); | ||
| Message message1 = restClient.get() | ||
| .uri(UriComponentsBuilder.fromUriString("http://localhost").port(port) | ||
| .queryParam("text", "hello").build().toUri()) | ||
| .retrieve() | ||
| .body(Message.class); | ||
| assertEquals(1, message1.id); | ||
| assertEquals("hello", message1.text); | ||
| Message message2 = restTemplate.getForObject( | ||
| UriComponentsBuilder.fromUriString("http://localhost").port(port) | ||
| .queryParam("text", "world").build().toUri(), | ||
| Message.class); | ||
|
|
||
| Message message2 = restClient.get() | ||
| .uri(UriComponentsBuilder.fromUriString("http://localhost").port(port) | ||
| .queryParam("text", "world").build().toUri()) | ||
| .retrieve() | ||
| .body(Message.class); | ||
| assertEquals(2, message2.id); | ||
| assertEquals("world", message2.text); | ||
|
|
||
| { | ||
| List<Message> messages = restTemplate.exchange( | ||
| UriComponentsBuilder.fromUriString("http://localhost").port(port) | ||
| .build().toUri(), | ||
| HttpMethod.GET, HttpEntity.EMPTY, | ||
| typedReference).getBody(); | ||
| List<Message> messages = restClient.get() | ||
| .uri(UriComponentsBuilder.fromUriString("http://localhost").port(port) | ||
| .build().toUri()) | ||
| .retrieve() | ||
| .body(typedReference); | ||
| assertEquals(2, messages.size()); | ||
| assertEquals(message1.id, messages.get(0).id); | ||
| assertEquals(message1.text, messages.get(0).text); | ||
|
|
@@ -55,12 +62,12 @@ void testWithTestContainers() { | |
| } | ||
|
|
||
| { | ||
| List<Message> messages = restTemplate.exchange( | ||
| UriComponentsBuilder.fromUriString("http://localhost").port(port) | ||
| List<Message> messages = restClient.get() | ||
| .uri(UriComponentsBuilder.fromUriString("http://localhost").port(port) | ||
| .queryParam("page", "1").queryParam("size", "1").build() | ||
| .toUri(), | ||
| HttpMethod.GET, HttpEntity.EMPTY, typedReference) | ||
| .getBody(); | ||
| .toUri()) | ||
| .retrieve() | ||
| .body(typedReference); | ||
| assertEquals(1, messages.size()); | ||
| assertEquals(message2.id, messages.get(0).id); | ||
| assertEquals(message2.text, messages.get(0).text); | ||
|
|
||
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