本文介绍了使用嵌入mongodb de.flapdoodle.embed.mongo的junit测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用flappoodle设置mongodb junit环境,当它尝试下载mongodb存档时得到了java.io.IOException.

I wanted to setup mongodb junit environment with flapdoodle and I got a java.io.IOException when it tries to download the mongodb archive.

我正在使用:

  • spring-data-mongodb:1.6.1.发布
  • de.flapdoodle.embed.mongo 1.47.3
  • org.springframework:4.0.3.发行版

我遇到以下错误:

de.flapdoodle.embed.process.exceptions.DistributionException: java.io.IOException: Could not open inputStream for http://downloads.mongodb.org/win32/mongodb-win32-i386-3.0.2.zip

原因:java.net.UnknownHostException:downloads.mongodb.org在java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)

Caused by: java.net.UnknownHostException: downloads.mongodb.orgat java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)

,它也指向mongodb-win32-i386-3.0.2.zip,但我使用的是Windows 64位.

and also its pointing to mongodb-win32-i386-3.0.2.zip but I am using windows 64 bit.

这是我的代码

package com.bosch.test;

import junit.framework.TestCase;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import com.bosch.in.model.Device;
import com.bosch.in.service.imp.DeviceServiceImp;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoClient;

import de.flapdoodle.embed.mongo.MongodExecutable;
import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;
import de.flapdoodle.embed.process.runtime.Network;

@ContextConfiguration(classes=ApplicationConfig.class,loader=AnnotationConfigContextLoader.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class DeviceServiceTest2 {

    private static final MongodStarter starter = MongodStarter
            .getDefaultInstance();

    private static MongodExecutable mongodExe;
    private static MongodProcess mongod;

    private static MongoClient mongo;


    private DeviceServiceImp deviceServiceImp;

    private MongoTemplate template;

    @BeforeClass
    public static void setUp() throws Exception {
        //System.out.println("1");
        mongodExe = starter.prepare(new MongodConfigBuilder()
                .version(Version.Main.V3_0)
                .net(new Net(12345, Network.localhostIsIPv6())).build());
        System.out.println("2");
        mongod = mongodExe.start();
        System.out.println("3");

        System.out.println("4");
        mongo = new MongoClient("12345", 12345);
        System.out.println("5");

    }

    @AfterClass
    public static void tearDown() throws Exception {
        mongod.stop();
        mongodExe.stop();
    }

    public Mongo getMongo() {
        return mongo;
    }

@Test
public void save(){

   System.out.println("1");

}

}

推荐答案

我认为问题是您使用的是默认MongodStarter,但它不知道您的代理配置(这是我的情况).您只需要配置mongodStarter.

I think the problem is that you are using a default MongodStarter that is not aware of your proxy configuration (it was my case).You just need to configure the mongodStarter.

代替使用

private static final MongodStarter starter = MongodStarter
        .getDefaultInstance();

您应该在setUp()中拥有类似的东西

you should have somthing like this in setUp()

Command command = Command.MongoD;

IRuntimeConfig runtimeConfig = new RuntimeConfigBuilder()
        .defaults(command)
        .artifactStore(new ArtifactStoreBuilder()
                .defaults(command)
                .download(new DownloadConfigBuilder()
                        .defaultsForCommand(command)
                        .proxyFactory(new HttpProxyFactory("proxy_host", 8080))))
        .build();

 MongodStarter starter = MongodStarter.getInstance(runtimeConfig);

此配置在 flapdoodle文档中有很好的解释.

这篇关于使用嵌入mongodb de.flapdoodle.embed.mongo的junit测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-17 01:26