SpringとSpringBootが非常に人気がある理由の1つは、優れたテストサポートです。Springの機能を使用せずにMockitoを使用したユニットテストと、Springコンテキストの初期化を使用した統合テストの両方を記述できます。
統合テストでは、リレーショナルデータベース、NoSQLデータベース、Kafkaなどの外部サービスとの対話が必要になる場合があります。テストするときは、これらのサービスをDockerコンテナにデプロイすると便利です。
テストコンテナ
Testcontainersのドキュメントから:
TestContainersは、人気のあるデータベース、Seleniumを備えたWebブラウザー、およびDockerコンテナーで実行できるその他すべての軽量で一時的なインスタンスを提供することにより、JUnitテストをサポートするJavaライブラリです。
Testcontainersを使用すると、次のようにシングルトンDockerコンテナを起動できます。
@SpringBootTest
@ContextConfiguration(initializers = {UserServiceIntegrationTest.Initializer.class})
class UserServiceIntegrationTest {
private static PostgreSQLContainer sqlContainer;
static {
sqlContainer = new PostgreSQLContainer("postgres:10.7")
.withDatabaseName("integration-tests-db")
.withUsername("sa")
.withPassword("sa");
sqlContainer.start();
}
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
TestPropertyValues.of(
"spring.datasource.url=" + sqlContainer.getJdbcUrl(),
"spring.datasource.username=" + sqlContainer.getUsername(),
"spring.datasource.password=" + sqlContainer.getPassword()
).applyTo(configurableApplicationContext.getEnvironment());
}
}
@Autowired
private UserService userService;
@Test
void shouldGetAllUsers() {
// test userService.getAllUsers()
}
}
これは非常に頻繁に使用されるため、コミュニティによる生活を簡素化するために、コミュニティによってスターターが作成されました-Testcontainers Spring BootStarter。
テストコンテナSpringBootスターター
Testcontainers-スターターはspring-cloud-starterに依存します。アプリケーションがSpringCloudスターターを使用しない場合は、テストの依存関係としてspring-cloud-starterを追加する必要があります。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<scope>test</scope>
</dependency>
また、データベースのライブラリを追加します。たとえば、Postgresqlを使用する場合:
<dependency>
<groupId>com.playtika.testcontainers</groupId>
<artifactId>embedded-postgresql</artifactId>
<scope>test</scope>
</dependency>
embedded-postgresql環境に
追加すると、次のプロパティが使用可能になります。
embedded.postgresql.port
embedded.postgresql.host
embedded.postgresql.schema
embedded.postgresql.user
embedded.postgresql.password
これらは、データソースの設定に使用できます。
通常、Dockerコンテナは統合テストにのみ使用され、ユニットテストには使用されません。プロファイルを使用すると、デフォルトでプロファイルを無効にし、統合テストでのみ有効にすることができます。
src/test/resources/bootstrap.properties
embedded.postgresql.enabled=false
src/test/resources/bootstrap-integration-test.properties
embedded.postgresql.enabled=true
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://${embedded.postgresql.host}:${embedded.postgresql.port}/${embedded.postgresql.schema}
spring.datasource.username=${embedded.postgresql.user}
spring.datasource.password=${embedded.postgresql.password}
これで、以下を使用して、統合テストプロファイルを使用して統合テストを実行できます
@ActiveProfiles。
@SpringBootTest
@ActiveProfiles("integration-test")
class UserServiceIntegrationTest {
@Autowired
private UserService userService;
@Test
void shouldGetAllUsers() {
// test userService.getAllUsers()
}
}
次のように、dockerイメージの特定のバージョンを指定できます。
src/test/resources/bootstrap-integration-test.properties
embedded.postgresql.dockerImage=postgres:10.7
embedded.postgresql.enabled=true
testcontainersスターターは、Postgresql、MariaDB、MongoDB、Redis、RabbitMQ、Kafka、Elasticsearchなどの最も人気のあるコンテナーのサポートをすでに提供しています。
驚いたことに、現在、MySQLは直接サポートされていません。ここで説明するように、これには簡単な回避策がありますが
Springでのアプリケーションコードのリファクタリング