Код: Выделить всё
@ActiveProfiles({ "test", "integration" })
@TestConfiguration(proxyBeanMethods = false)
public class TestContainersConfiguration {
@Bean
@ServiceConnection
public MongoDBContainer mongoDbContainer() throws UnsupportedOperationException, IOException, InterruptedException {
var mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.4"));
mongoDBContainer.start();
mongoDBContainer.copyFileToContainer(MountableFile.forClasspathResource("testResources.json"),
"/publications.json");
mongoDBContainer.execInContainer("mongoimport",
"--db", "documents",
"--mode", "upsert",
"--collection", "dspacePublication", "--file",
"/publications.json", "--jsonArray", "--quiet");
return mongoDBContainer;
}
}
Код: Выделить всё
@ActiveProfiles({ "test", "integration" })
@Import(TestContainersConfiguration.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public abstract class AbstractIntegrationTest {
@Autowired
protected MongoTemplate mongoTemplate;
@Autowired
protected WebApplicationContext webApplicationContext;
@Autowired
protected MongoDBContainer mongoDBContainer;
@BeforeEach
protected void beforeEach() throws UnsupportedOperationException, IOException, InterruptedException {
assertTrue(mongoDBContainer.isRunning());
}
}
Код: Выделить всё
@ActiveProfiles({ "test", "integration" })
@EnableAutoConfiguration
public class SearchControllerIT extends AbstractIntegrationTest {
private List publications;
@BeforeEach
public void setUp() throws Exception {
this.publications = super.mongoTemplate.findAll(Publication.class);
assertFalse(publications.isEmpty(), "an empty list of publications was returned when querying test MongoDB!");
}
// test methods
}
Код: Выделить всё
spring.data.mongodb.database=${SPRING_DATA_MONGODB_DATABASE:documents}
spring.data.mongodb.host=${SPRING_DATA_MONGODB_HOST:mongodb}
spring.data.mongodb.port=${SPRING_DATA_MONGODB_PORT:27017}
Код: Выделить всё
spring.data.mongodb.uri=${SPRING_DATA_MONGODB_URI:mongodb://mongodb:27017/documents}
Он выглядит как «@ServiceConnection» не переопределяет Spring.data.mongodb.uri, поэтому приоритет имеет файл из application.properties.
Как запустить интеграционные тесты, сохраняя при этом Spring.data.mongodb.uri в приложении .свойства?
Подробнее здесь: https://stackoverflow.com/questions/787 ... pring-data