在我的 Spring 应用程序中,我有一个依赖于 MongoDb 的部分,我必须对其进行测试。
我希望能够在本地和构建服务器上运行测试。
目前我正在尝试这样做,添加
testCompile group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '2.2.0'
作为依赖项。
此包从 mongodb.org 下载 mongo 可执行文件,提取它们并将其存储在本地。 虽然这在本地很有效,但在构建服务器上肯定会失败,因为它无法访问远程站点。
所以我想更改配置,以使用在工件存储库中找到的可执行文件,或者如果需要的话,使用已添加到项目存储库中的可执行文件。
一段有趣的代码似乎是 de.flapdoodle.embed.mongo.config.DownloadConfigBuilder
它表明可能使用了某些环境变量
EMBEDDED_MONGO_ARTIFACTS
它显示默认的下载源,但路径不是绝对的。所以我不知道我的镜子需要如何构造。访问链接,我将被重定向。
private static class PlatformDependentDownloadPath implements IDownloadPath { @Override public String getPath(Distribution distribution) { if (distribution.getPlatform()==Platform.Windows) { return "https://downloads.mongodb.org/"; } return "https://fastdl.mongodb.org/"; } }
然后在 flampdoodle 的文档中您会发现:
- Customize Download URL
... Command command = Command.MongoD; IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder() .defaults(command) .artifactStore(new ExtractedArtifactStoreBuilder() .defaults(command) .download(new DownloadConfigBuilder() .defaultsForCommand(command) .downloadPath("http://my.custom.download.domain/"))) .build(); ...
但是,这个downloadPath似乎并不是绝对的。
简单地说:
- 我需要如何构建我的下载镜像
- 如何配置下载路径以使用我的镜像 - 尤其是 Spring 自动配置
最佳答案
我知道这个问题已经有一段时间了,但也许它有帮助。
我设法通过以下@Configuration(在您的测试包中)手动配置它。
@Configuration
public class EmbeddedMongoDBConfig {
@Bean
public IRuntimeConfig embeddedMongoRuntimeConfig() {
final Command command = Command.MongoD;
final IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
.defaults(command)
.artifactStore(new ExtractedArtifactStoreBuilder()
.defaults(command)
.downloader(new Downloader())
.download(new DownloadConfigBuilder()
.downloadPath("http://host/path/to/mongodb/server/mongodb/3.5.5/")
.fileNaming(new UserTempNaming())
.downloadPrefix("")
.artifactStorePath(new PlatformTempDir())
.progressListener(new Slf4jProgressListener(log))
.userAgent("")
.packageResolver(new IPackageResolver() {
// this apparently is expected to return the name of the executable (in the download package)
@Override
public FileSet getFileSet(Distribution distribution) {
String filename = (distribution.getPlatform().isUnixLike()) ? "bin/mongod" : "bin/mongod.exe";
return FileSet.builder()
.addEntry(FileType.Executable, filename)
.build();
}
@Override
public ArchiveType getArchiveType(Distribution distribution) {
if(distribution.getPlatform().isUnixLike()) return ArchiveType.TGZ;
else return ArchiveType.ZIP;
}
// the path (appended to the download link above)
@Override
public String getPath(Distribution distribution) {
return (distribution.getPlatform().isUnixLike()) ? "mongodb-3.5.5-linux-x86_64.tgz" : "mongodb-3.5.5-win32-x86_64.zip";
}
})
.build())
.executableNaming(new UserTempNaming())
.build())
.build();
return runtimeConfig;
}
关于spring - 配置 fladoodles 嵌入 mongodb 来构建管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63613461/