Switch Multi-Store sample to Testcontainers.

This commit is contained in:
Oliver Drotbohm
2024-08-16 12:46:56 +02:00
parent dba65e82c0
commit ad27b23415
4 changed files with 38 additions and 11 deletions

View File

@@ -30,6 +30,20 @@
<scope>test</scope>
</dependency>
<!-- Testcontainers -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2021 the original author or authors.
* Copyright 2014-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,34 +15,46 @@
*/
package example.springdata.multistore;
import static org.assertj.core.api.Assertions.*;
import example.springdata.multistore.customer.Customer;
import example.springdata.multistore.shop.Order;
import org.junit.Test;
import org.junit.runner.RunWith;
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.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.repository.support.Repositories;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.utility.DockerImageName;
/**
* Integration test to check repository interfaces are assigned to the correct store modules.
*
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationConfigurationTest {
class ApplicationConfigurationTest {
@Autowired ApplicationContext context;
@TestConfiguration
static class Infrastructure {
@Bean
@ServiceConnection
MongoDBContainer mongoDBContainer() {
return new MongoDBContainer(DockerImageName.parse("mongodb/mongodb-community-server"));
}
}
@Test
public void repositoriesAreAssignedToAppropriateStores() {
void repositoriesAreAssignedToAppropriateStores() {
var repositories = new Repositories(context);

View File

@@ -13,4 +13,4 @@
<appender-ref ref="console" />
</root>
</configuration>
</configuration>

View File

@@ -93,8 +93,9 @@
<artifactId>webjars-locator-core</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Testcontainers -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>