forked from hantsy/spring-reactive-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored boot-data-r2dbc-postgresql to use testContainers for @Data…
…R2dbcTest
- Loading branch information
Showing
6 changed files
with
129 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
boot-data-r2dbc-postgresql/src/test/java/com/example/demo/PostRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.example.demo; | ||
|
||
|
||
import io.r2dbc.spi.ConnectionFactory; | ||
import org.junit.Before; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.data.r2dbc.DataR2dbcTest; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.data.r2dbc.connectionfactory.init.CompositeDatabasePopulator; | ||
import org.springframework.data.r2dbc.connectionfactory.init.ConnectionFactoryInitializer; | ||
import org.springframework.data.r2dbc.connectionfactory.init.ResourceDatabasePopulator; | ||
import org.springframework.data.r2dbc.core.DatabaseClient; | ||
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.junit.jupiter.Testcontainers; | ||
import reactor.test.StepVerifier; | ||
|
||
import java.time.Duration; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
||
// see: https://github.com/spring-projects-experimental/spring-boot-r2dbc/issues/68 | ||
@DataR2dbcTest | ||
@Testcontainers | ||
@TestInstance(TestInstance.Lifecycle.PER_METHOD) | ||
public class PostRepositoryTest { | ||
|
||
@Container | ||
static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer(); | ||
|
||
@DynamicPropertySource | ||
static void resgiterDynamicProperties(DynamicPropertyRegistry registry) { | ||
registry.add("spring.r2dbc.url", () -> "r2dbc:postgresql://" | ||
+ postgreSQLContainer.getHost() + ":" + postgreSQLContainer.getFirstMappedPort() | ||
+ "/" + postgreSQLContainer.getDatabaseName()); | ||
registry.add("spring.r2dbc.username", () -> postgreSQLContainer.getUsername()); | ||
registry.add("spring.r2dbc.password", () -> postgreSQLContainer.getPassword()); | ||
} | ||
|
||
@TestConfiguration | ||
static class TestConfig { | ||
@Bean | ||
public ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) { | ||
|
||
ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer(); | ||
initializer.setConnectionFactory(connectionFactory); | ||
|
||
CompositeDatabasePopulator populator = new CompositeDatabasePopulator(); | ||
populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("schema.sql"))); | ||
populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("data.sql"))); | ||
initializer.setDatabasePopulator(populator); | ||
|
||
return initializer; | ||
} | ||
} | ||
|
||
@Autowired | ||
DatabaseClient client; | ||
|
||
@Autowired | ||
PostRepository posts; | ||
|
||
@Test | ||
public void testDatabaseClientExisted() { | ||
assertNotNull(client); | ||
} | ||
|
||
@Test | ||
public void testPostRepositoryExisted() { | ||
assertNotNull(posts); | ||
} | ||
|
||
|
||
@Test | ||
public void existedOneItemInPosts() { | ||
assertThat(this.posts.count().block()).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
public void testInsertAndQuery() { | ||
this.client.insert() | ||
.into("posts") | ||
//.nullValue("id", Integer.class) | ||
.value("title", "mytesttitle") | ||
.value("content", "testcontent") | ||
.then().block(Duration.ofSeconds(5)); | ||
|
||
this.posts.findByTitleContains("%testtitle") | ||
.take(1) | ||
.as(StepVerifier::create) | ||
.consumeNextWith(p -> assertEquals("mytesttitle", p.getTitle())) | ||
.verifyComplete(); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DELETE FROM posts; | ||
INSERT INTO posts (title, content) VALUES ('post one in data.sql', 'content of post one in data.sql'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CREATE SEQUENCE table_name_id_seq; | ||
|
||
CREATE TABLE IF NOT EXISTS posts( | ||
id integer NOT NULL PRIMARY KEY DEFAULT nextval('table_name_id_seq') , | ||
title VARCHAR(255) NOT NULL, | ||
content VARCHAR(255) NOT NULL | ||
); | ||
|
||
ALTER SEQUENCE table_name_id_seq OWNED BY posts.id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters