使用embed mongodb de.flapdoodle.embed.mongo的junit测试用例

时间:2015-05-22 05:24:28

标签: mongodb junit spring-data-mongodb

我想用flapdoodle设置mongodb junit环境,当我试图下载mongodb档案时我得到java.io.IOException

我正在使用:

  • spring-data-mongodb:1.6.1.RELEASE
  • de.flapdoodle.embed.mongo 1.47.3
  • org.springframework:4.0.3.RELEASE

我收到以下错误:

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)

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

这是我的代码

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");

}

}

1 个答案:

答案 0 :(得分:2)

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

而不是使用

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

你应该在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 doc上详细解释了此配置。