问题描述
在我的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/"; } }
然后在flappoodle的文档中找到:
- 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;
}